How to Stop DataGridView editing after checked cell?

会有一股神秘感。 提交于 2019-12-11 00:52:33

问题


I use ContexMenuStrip on DataGridView to delete some rows but it doesn't work correctly.

Every time if I checked 3 rows, after selecting the ContexMenuStrip it only deletes 2 rows. When I do this code without the ContexMenuStrip (by Button) that works correctly.

When I see the behavior I understand current row is editing but doesn't finish. After double clicking on the current row to stop editing my ContexMenuStrip works correctly.

How to stop editing after checking the CheckBox?


回答1:


When a cell has been selected and edited, the DataGridView property IsCurrentCellDirty is set to True. If you catch the event handler when this state changes on a DataGridViewCheckBoxCell, you can call DataGridView.EndEdit() to finalize those changes immediately.

this.dataGridView1.CurrentCellDirtyStateChanged += DataGridView1_CurrentCellDirtyStateChanged;

private void DataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    if (this.dataGridView1.IsCurrentCellDirty && this.dataGridView1.CurrentCell is DataGridViewCheckBoxCell)
    {
        this.dataGridView1.EndEdit();
    }
}

Further explanation:

Behind the scenes, DataGridView.IsCurrentCellDirty is updated whenever you edit the current cell. The first line of code above allows you to attach to the CurrentCellDirtyStateChanged event your own event handler (DataGridView1_CurrentCellDirtyStateChanged) . So whenever the cell becomes dirty, behind the scenes will call the base level event and then your method as well. Without that line, your method will not be called. The += operator is what attaches your method to the event's call-chain.

For example, adding the following handlers:

this.dataGridView1.CurrentCellDirtyStateChanged += DataGridView1_Example1;
// this.dataGridView1.CurrentCellDirtyStateChanged += DataGridView1_Example2;
this.dataGridView1.CurrentCellDirtyStateChanged += DataGridView1_Example3;

private void DataGridView1_Example1(object sender, EventArgs e)
{
    Console.WriteLine("Example 1");
}

private void DataGridView1_Example2(object sender, EventArgs e)
{
    Console.WriteLine("Example 2");
}

private void DataGridView1_Example3(object sender, EventArgs e)
{
    Console.WriteLine("Example 3");
}

When the dirty state changes, you'll see the following output. Notice the 2nd event handler was excluded:

// Example 1
// Example 3



回答2:


There's a small problem in the code proposed by OhBeWise. It works for Mouse-Clicks. But if you toggle the checkbox with the Space key you can't toggle the checkbox again without manually switching the current cell first. With a small change it will work:

private void DataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    if (this.dataGridView1.IsCurrentCellDirty && this.dataGridView1.CurrentCell is DataGridViewCheckBoxCell)
    {
        this.dataGridView1.EndEdit();

        DataGridViewCell currentCell = this.dataGridView1.CurrentCell;

        this.dataGridView1.CurrentCell = null;
        this.dataGridView1.CurrentCell = currentCell;
    }
}


来源:https://stackoverflow.com/questions/43530300/how-to-stop-datagridview-editing-after-checked-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!