我有一个命令类(希望)使用DataGridViewRow作为成员来执行和撤消。当我尝试将传入的dgvr分配给构造函数中的成员行(例如_row = r;)时,似乎我得到了一个引用,而不是一个新的副本,使得撤销操作无法进行(原始的dgvr被删除了然后成员行变空了。
我还尝试通过msdn“复制”该行,如下所示:
public DataGridViewRow CloneWithValues(DataGridViewRow row)
{
DataGridViewRow clonedRow = (DataGridViewRow)row.Clone();
for (Int32 index = 0; index < row.Cells.Count; index++)
{
clonedRow.Cells[index].Value = row.Cells[index].Value;
}
return clonedRow;
}
然而,这似乎正在抛弃架构,因为我无法按名称访问字段(例如_row.Cells [“critical”]。值)。
如何根据DataGridViewRow的值制作副本?
答案 0 :(得分:1)
我会尝试更简单的方法,在数据表级别,调用datatable.Rows.Add并传递修改后的datarow.ItemArray。
如果你仍然对你的表有一个引用,这应该是我认为的网格数据源。我在我的项目中使用了它,通常工作正常。