简体   繁体   中英

Cancel Edit Cell on Double Click DataGridView C# WinForm

I have double click event in DataGridView like below :

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 :

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() ? 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.

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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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