c# - 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.
推荐阅读
- angular - 我不知道这是如何工作的。Angular 中的 @Input()
- modelica - JModelica FMU 中的方向导数
- python - python中Andor的高速显示
- autohotkey - 是否可以模拟在 LTR 和 RTL 环境中都有效的“返回一个角色”动作?
- python - 导入“mp3play”插件时出错
- apache - 将规则从旧链接重写到同一域上的新链接
- css - 菜单背景颜色 Wordpress 仅用于主页
- java - 如何将对象列表简化为另一个列表?
- android - popbackstack()不工作
- xamarin - Xamarin 表单 MatserdetailPage 不会显示汉堡包图标