DataGridView Rows.Insert 插入DataGridViewRow对象到首行 Row对象 被偷换的 问题。。。。加分。。。
本帖最后由 shigaofei1 于 2012-12-14 21:01:42 编辑 问题是这样的,简单来说就是要在第一行插入新行。。。
但是行的代码如下:
public class dgvRow:DataGridViewRow
{
public dgvRow(RowDataState rs)
{
RowState = new dgvRowState(this, rs);
}
public dgvRow()
{
}
public dgvRowState RowState { get; set; }
}
public enum RowDataState : byte
{
_none,
_new,
_db,
_edit
}
//从用户界面添加
public void AddNewRow_UserAdd(int index)
{
dgvRow newrow = new dgvRow(RowDataState._new);
//newrow.CreateCells(dgv);
//dgv.Rows.Insert(index, 1);
dgv.Rows.Insert(0, newrow);
//dgv.Rows.Insert(index, newrow);
}
private void Form7_Load(object sender, EventArgs e)
{
this.dataGridView1.AllowUserToAddRows = true;
DataGridViewRow dgvr = new DataGridViewRow();
this.dataGridView1.Rows.Insert(0 , dgvr);
if (this.dataGridView1.Rows[0] == dgvr)
{
MessageBox.Show("Test");
}
}
this.dataGridView1.Rows[0]
{DataGridViewRow { Index=0 }}
base {System.Windows.Forms.DataGridViewBand}: {DataGridViewRow { Index=0 }}
AccessibilityObject: {System.Windows.Forms.DataGridViewRow.DataGridViewRowAccessibleObject}
Cells: {System.Windows.Forms.DataGridViewCellCollection}
ContextMenuStrip: null
DataBoundItem: null
DefaultCellStyle: {DataGridViewCellStyle { }}
Displayed: true
DividerHeight: 0
ErrorText: ""
Frozen: false
HeaderCell: {DataGridViewRowHeaderCell { RowIndex=0 }}
Height: 23
InheritedStyle: {DataGridViewCellStyle { BackColor=Color [Window], ForeColor=Color [ControlText], SelectionBackColor=Color [Highlight], SelectionForeColor=Color [HighlightText], Font=[Font: Name=宋体, Size=9, Units=3, GdiCharSet=134, GdiVerticalFont=False], WrapMode=False, Alignment=MiddleLeft }}
IsNewRow: false
MinimumHeight: 3
ReadOnly: false
Resizable: True
Selected: false
State: Displayed
[解决办法]
Visible
Visible: true
-----------------------
dgvr
{DataGridViewRow { Index=-1 }}
base {System.Windows.Forms.DataGridViewBand}: {DataGridViewRow { Index=-1 }}
AccessibilityObject: {System.Windows.Forms.DataGridViewRow.DataGridViewRowAccessibleObject}
Cells: {System.Windows.Forms.DataGridViewCellCollection}
ContextMenuStrip: “dgvr.ContextMenuStrip”引发了“System.InvalidOperationException”类型的异常
DataBoundItem: null
DefaultCellStyle: {DataGridViewCellStyle { }}
Displayed: “dgvr.Displayed”引发了“System.InvalidOperationException”类型的异常
DividerHeight: 0
ErrorText: “dgvr.ErrorText”引发了“System.InvalidOperationException”类型的异常
Frozen: “dgvr.Frozen”引发了“System.InvalidOperationException”类型的异常
HeaderCell: {DataGridViewRowHeaderCell { RowIndex=-1 }}
Height: 23
InheritedStyle: “dgvr.InheritedStyle”引发了“System.InvalidOperationException”类型的异常
IsNewRow: false
MinimumHeight: 3
ReadOnly: “dgvr.ReadOnly”引发了“System.InvalidOperationException”类型的异常
Resizable: “dgvr.Resizable”引发了“System.InvalidOperationException”类型的异常
Selected: “dgvr.Selected”引发了“System.InvalidOperationException”类型的异常
State: “dgvr.State”引发了“System.InvalidOperationException”类型的异常
Visible: “dgvr.Visible”引发了“System.InvalidOperationException”类型的异常