All,I knew we can set a column editable for a DataGridView
.
And when finish editing the cell. the CellEndEdit
event would be triggered.
But I just want
Before BeginEdit
. Set a variable to identify if current state is edit mode.
bBeginEdit = true;
dgvFileList.BeginEdit(false);
In the Form_Click event
if (bBeginEdit)
{
dgvFileList.EndEdit();
bBeginEdit = false;
}
Thanks,
Joe
CellEndEdit() causes the event to be fired only if the cell was in edit mode (see Joe.wang's response). You can simply preceed CellEndEdit() with CellBeginEdit() to enter Edit mode (code from CellContentClick-handler, PickNewFont() is a wrapper for the FontDialog):
[...]
else if (String.Compare(rowName, "Font name") == 0) // user clicks on Font-row
{
dgvConfigSettings.BeginEdit(true);
Font newFont = PickNewFont(fontName, fontSize, fontStyle);
dgvConfigSettings.CurrentCell.Tag = newFont; // a bit dirty.... but that way we can pick-up the font in the panel-handler more easily
dgvConfigSettings.CurrentCell.Value = newFont.Name.ToString();
dgvConfigSettings.EndEdit();
}
Try using the HitTest function in the MouseDown event of the grid:
void dgvFileList_MouseDown(object sender, MouseEventArgs e) {
DataGridView.HitTestInfo hit = dgvFileList.HitTest(e.X, e.Y);
if (hit.RowIndex < 0 | hit.ColumnIndex < 0) {
dgvFileList.EndEdit();
}
}
Clicking outside the DataGridView control would require hitting a focusable control.