Why didn't trigger the CellEndEdit event of DataGridView

后端 未结 3 1854
遇见更好的自我
遇见更好的自我 2021-01-22 20:25

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

相关标签:
3条回答
  • 2021-01-22 20:43

    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

    0 讨论(0)
  • 2021-01-22 21:03

    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();
    }
    
    0 讨论(0)
  • 2021-01-22 21:04

    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.

    0 讨论(0)
提交回复
热议问题