CurrentCellDirtyStateChanged在更改单元格之前和之后给我.为什么?
本文关键字:之后 为什么 单元格 CurrentCellDirtyStateChanged | 更新日期: 2023-09-27 18:25:31
我正试图通过使用CurrentCellDirtyStateChanged
事件来捕获数据网格视图中所做的更改。当用户更改数据网格视图中的单元格时,我会捕获"之前"图像并将其保存到List
。问题是,不知怎么的,我的代码将before和after图像都添加到了我的列表中,所以我最终得到了两个条目。当我完成调试时,程序似乎正在执行我的方法的最后三条语句两次。我做错什么了吗?这是我的代码:
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
// Create a list to hold before changes.
changeList = new List<ChangeLogObject>();
var beforeChange = new ChangeLogObject
{
UserName = Environment.UserName,
TimeChanged = DateTime.Now,
State = "Before",
Mode = "Lesson",
Hive = this.ddlHive.Text,
Project = this.ddlProjects.Text,
Mission = this.ddlMissions.Text,
Module = this.ddlModules.Text,
Course = this.ddlCourses.Text,
Frame = (int)this.dataGridView1.CurrentRow.Cells[1].Value,
LayerTitle = (string)this.dataGridView1.CurrentRow.Cells[2].Value,
LayerText = (string)this.dataGridView1.CurrentRow.Cells[3].Value,
GraphicNumber = (string)this.dataGridView1.CurrentRow.Cells[4].Value,
Height = (int)this.dataGridView1.CurrentRow.Cells[5].Value,
Width = (int)this.dataGridView1.CurrentRow.Cells[6].Value,
X = (int)this.dataGridView1.CurrentRow.Cells[7].Value,
Y = (int)this.dataGridView1.CurrentRow.Cells[8].Value
};
this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
this.dataGridView1.EndEdit();
this.changeList.Add(beforeChange);
}
在事件处理方法中,只检查单元格是否脏,如果是,则启动CommitEdit(=Cellvaluechanged ev.handling方法)链接:http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged(v=vs.110).aspx