CurrentCellDirtyStateChanged在更改单元格之前和之后给出了我。为什么?

时间:2014-10-02 16:29:26

标签: c# datagridview

我试图通过使用CurrentCellDirtyStateChanged事件来捕获数据网格视图中所做的更改。当用户对datagridview中的单元格进行更改时,我会在之前捕获'将图像保存为List。问题是,不知何故,我的代码将前后图像添加到我的列表中,所以我最终得到了两个条目。当我逐步调试时,程序似乎正在执行我的方法的最后三个语句两次。有什么我做错了吗?这是我的代码:

    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);
    }

1 个答案:

答案 0 :(得分:1)

事件处理方法中的

只检查单元格是否脏,如果是,则触发CommitEdit(= Cellvaluechanged ev。处理方法) 链接: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged(v=vs.110).aspx