检查单元格后如何停止DataGridView编辑? [英] How to Stop DataGridView editing after checked cell?

查看:208
本文介绍了检查单元格后如何停止DataGridView编辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 DataGridView 上使用 ContexMenuStrip 删除了一些行,但是它不能正常工作。

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

每次检查3行时,选择 ContexMenuStrip 后,它只会删除2行。当我执行此代码时,没有正常工作的 ContexMenuStrip (通过 Button )。

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.

当我看到该行为时,我知道当前行正在编辑但没有完成。双击当前行以停止编辑后,我的 ContexMenuStrip 可以正常工作。

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.

在选中 CheckBox 后如何停止编辑?

How to stop editing after checking the CheckBox?

推荐答案

选中并编辑一个单元格后, DataGridView 属性 IsCurrentCellDirty 设置为 True 。如果在 DataGridViewCheckBoxCell 上更改此状态时捕获事件处理程序,则可以调用 DataGridView.EndEdit() 即可立即完成这些更改。

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:

在幕后, DataGridView.IsCurrentCellDirty 会在您每次编辑当前单元格时进行更新。上面的第一行代码使您可以将自己的事件处理程序( DataGridView1_CurrentCellDirtyStateChanged )附加到 CurrentCellDirtyStateChanged 事件。因此,每当单元变脏时,幕后都会调用基本级事件,然后再调用您的方法。没有该行,将不会调用您的方法。 + = 运算符是将方法附加到事件的调用链的方法。

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

这篇关于检查单元格后如何停止DataGridView编辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆