According to the documenatation, DataGridView.CancelEdit()
“Cancels edit mode for the currently selected cell and discards any changes.”
Why then, does DataGridView.CurrentCell.IsInEditMode return true after CancelEdit() has been invoked?
To see this, create a simple Win App with a unbound DataGridView (dgv1) and a button (button1).
Add the following code.
privatevoid Form1_Load(object sender, EventArgs e)
{
dgv1.EditMode = DataGridViewEditMode.EditProgrammatically;
dgv1.ColumnCount = 3;
dgv1.RowCount = 3;
}
privatevoid button1_Click(object sender, EventArgs e)
{
dgv1.CurrentCell = dgv1[0, 0];
dgv1.BeginEdit(true);
Debug.WriteLine("Status: " + dgv1.CurrentCell.IsInEditMode);
if (dgv1.CancelEdit())
Debug.WriteLine("CancelEdit() successful”);
else
Debug.WriteLine("CancelEdit() failed”);
Debug.WriteLine("Status: " + dgv1.CurrentCell.IsInEditMode); //(*)
dgv1.EndEdit();
Debug.WriteLine("Status: " + dgv1.CurrentCell.IsInEditMode);
}
The output is
Edit mode: True
CancelEdit() successful
Edit mode: True //(*)
Edit mode: False
After executing the line marked (*) the cell should not be in edit mode, but IsInEditMode returns true. It’s not until we execute EndEdit() that IsInEditMode returns false.
Is this a bug or have I got hold of the wrong end of the stick?
Thanks
Mort.