DataGridView.CancelEdit() doesn’t.

  • Thread starter Thread starter Mortimer Muggle
  • Start date Start date
M

Mortimer Muggle

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



private void Form1_Load(object sender, EventArgs e)

{

dgv1.EditMode = DataGridViewEditMode.EditProgrammatically;

dgv1.ColumnCount = 3;

dgv1.RowCount = 3;

}



private void 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.

Continue reading...
 
Back
Top