简体   繁体   English

取消双击DataGridView C#WinForm上的编辑单元格

[英]Cancel Edit Cell on Double Click DataGridView C# WinForm

I have double click event in DataGridView like below : 我在DataGridView中有双击事件,如下所示:

private void gridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    // put something here to cancel the edit

    Form dialogForm = new dialogContainerForm(username);
    dialogForm.ShowDialog(this);
}

When this double click fired, it will call another form, and when this child form closed, it will load the grid : 触发此双击时,它将调用另一个表单,并且当此子表单关闭时,它将加载网格:

public void callWhenChildClick(List<string> codes)
{
    //some code here
    Grid_Load();
}

I have cell validating which always fired when this Grid_Load() called : 我有单元格验证,当此Grid_Load()调用时,它始终会触发:

private void gridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    string code = e.FormattedValue.ToString();
    string headerText = gridView.Columns[e.ColumnIndex].HeaderText;

    if (!headerText.Equals("No. Transaksi")) return;

    if (string.IsNullOrEmpty(code))
    {
        MessageBox.Show("No. Transaksi tidak boleh kosong!");
        e.Cancel = true;
    }
}

How to ignore this cell validating just for this case Grid_Load() ? 如何忽略此单元格仅针对Grid_Load()进行验证? Or is there any function to cancel the edit and ignore validating when the cell double clicked? 还是有任何功能可以在单元格双击时取消编辑并忽略验证?

If you need to prevent an event handler from executing, it can be temporarily removed from the Object and then re-applied when you want it to run again. 如果需要阻止事件处理程序执行,可以将其暂时从Object中删除,然后在希望再次运行时重新应用。

To disable (actually remove) an event handler, add the code: 要禁用(实际上删除)事件处理程序,请添加代码:

gridView.CellValidating -= gridView_CellValidating

After this line you can run what ever code you want to without it causing the event handler to execute. 在此行之后,您可以运行所需的任何代码,而不会导致事件处理程序执行。

The event handler can then be reset or added afresh by adding the line: 然后可以通过添加以下行来重置或重新添加事件处理程序:

gridView.CellValidating += gridView_CellValidating

Note: Each time you are looking to add an event handler like above, you should also precede the call with a remove action to prevent the event handler from executing more than once (or more than the expected number of times). 注意:每次您希望像上面那样添加事件处理程序时,还应该在调用之前执行remove操作,以防止事件处理程序执行多次(或超过预期的次数)。 If the event handler hasn't been added and you attempt to remove it, there will be no side-effects, however, multiple additions of the same event handler will cause the event handler to execute multiple times. 如果尚未添加事件处理程序,而您尝试将其删除,则不会有副作用,但是,多次添加同一事件处理程序将导致事件处理程序执行多次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM