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);
}
问题描述:
我自己在AddNewRow_UserAdd方法中 实例化的 newrow
每次都不能插入到表中。。。
每次插入完成之后,表里面的并不是我插入的那个对象。。。
这是怎么回事。。。
程序也不报错。。。
二次描述:
简单来说就是
第一行:dgvRow newrow = new dgvRow(RowDataState._new);
第二行:dgv.Rows.Insert(0,newrow);
第三行:if(dgv.Rows[0]!=newrow){MessageBox(为什么这里是不等于!!!);}
为什么第二行的Insert方法插入之后我检查dgv.Rows[0]中的对象不是newrow而是一个其他的未知的不知道从哪里冒出来的dgvRow类型的对象,不知道怎么Insert这个方法会替换掉我的那个第一行实例化的newrow对象吗?
即newrow!=dgv.Rows[0]
???这是什么原因。。。请教 控件大神
[解决办法]
dgvRowState是个什么东西 麻烦楼主给解释一下
下面是我测试的结果 因为lz代码可能给的不完整 导致在写类的时候无法得到dgvRowState 所以编译无法通过 我的做法是这样写的
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”类型的异常