I have a datagridview that contains 5 columns, in the code I also added datagridview1__CellValidating event as well. Currently when a user tries to modify a cell value and then leaves the cell the datagridview1__CellValidating event is triggered but I don't want datagridview1__CellValidating to be always triggered.
In my case, lets say that I am trying to modify the value of a column 3 and then I click\\select the cell in the column 2 or column 1 of the current row. In this case I don't want datagridview1__CellValidating to be triggered. Is there any condition that I can add in datagridview1__CellValidating?
But I want datagridview1__CellValidating to be executed after modifying the value of the column 2 if I try to select the cell of a column 4 or 5 in my current row.
Is there any solution\\workaround for this?
use CellValidating
event in DataGridView
and Cancel editing for Column 1 and 2.
private void DataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
DataGridViewCellCollection cells = dataGridView1.Rows[e.RowIndex].Cells;
foreach(DataGridViewCell cell in cells)
{
if(cell.ColumnIndex == 0 || cell.ColumnIndex == 1)
{
e.Cancel = true;
}
}
}
And override the button to close the form because with e.Cancel = true
you can not close the form.
protected override void OnFormClosing(FormClosingEventArgs e)
{
e.Cancel = false;
base.OnFormClosing(e);
}
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.