Datagridview: How to set a cell in editing mode?

前端 未结 5 1782
梦毁少年i
梦毁少年i 2020-11-28 09:50

I need to programmatically set a cell in editing mode. I know that setting that cell as CurrentCell and then call the method BeginEdit(bool), it should happen, but in my cas

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 10:40

    Setting the CurrentCell and then calling BeginEdit(true) works well for me.

    The following code shows an eventHandler for the KeyDown event that sets a cell to be editable.

    My example only implements one of the required key press overrides but in theory the others should work the same. (and I'm always setting the [0][0] cell to be editable but any other cell should work)

        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Tab && dataGridView1.CurrentCell.ColumnIndex == 1)
            {
                e.Handled = true;
                DataGridViewCell cell = dataGridView1.Rows[0].Cells[0];
                dataGridView1.CurrentCell = cell;
                dataGridView1.BeginEdit(true);               
            }
        }
    

    If you haven't found it previously, the DataGridView FAQ is a great resource, written by the program manager for the DataGridView control, which covers most of what you could want to do with the control.

提交回复
热议问题