我的datagrid有两个文本框列和两个按钮列。 我试图当用户点击按钮时,相邻的文本框单元格将处于编辑模式,光标将放在那里。
我的文本框列处于只读模式。
所以我在单元格中输入了代码
private void dgView_CellEnter(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex == 2) { DataGridView dgv = (DataGridView)sender; DataGridViewTextBoxCell textBoxCell = dgv.CurrentCell as DataGridViewTextBoxCell; if (textBoxCell != null) { dgv.BeginEdit(true); } } }我没有得到错误但我的目标没有实现。 文本框单元格未进入编辑模式,并且光标未放置在文本框单元格上。 因此,当我点击按钮然后相邻的文本框单元格将进入编辑模式并且光标将在那里闪烁时,请告诉我并纠正我的代码。
My datagrid has two textbox columns and two button column. I was trying that when user clicks on button then adjacent textbox cell will be in edit mode and cursor will be placed there.
My textbox columns are in readonly mode.
So I wrote code in cell enter that
private void dgView_CellEnter(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex == 2) { DataGridView dgv = (DataGridView)sender; DataGridViewTextBoxCell textBoxCell = dgv.CurrentCell as DataGridViewTextBoxCell; if (textBoxCell != null) { dgv.BeginEdit(true); } } }I am not getting error but my objective is not fulfill. Textbox cell is not getting in edit mode and cursor is not being placed on the textbox cell. So please tell me and rectify my code as result when I will click on button then adjacent textbox cell will come in edit mode and cursor will blink there.
最满意答案
尝试使用CellClick事件。 你真的不需要调用Focus()只需设置CurrentCell和BeginEdit :
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex == 2) { DataGridView dgv = (DataGridView)sender; DataGridViewCell textBoxCell = dgv.Rows[e.RowIndex].Cells[col_index]; if (textBoxCell != null) { dataGridView1.CurrentCell = textBoxCell; dgv.BeginEdit(true); } } }Try using the CellClick event. You don't really need to call Focus() just set the CurrentCell and BeginEdit:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex == 2) { DataGridView dgv = (DataGridView)sender; DataGridViewCell textBoxCell = dgv.Rows[e.RowIndex].Cells[col_index]; if (textBoxCell != null) { dataGridView1.CurrentCell = textBoxCell; dgv.BeginEdit(true); } } }更多推荐
发布评论