DataGridViewTextBoxColumn,将光标设置在单元格内容的开头(DataGridViewTextBoxColumn, set the cursor at the beginning

编程入门 行业动态 更新时间:2024-10-16 00:25:56
DataGridViewTextBoxColumn,将光标设置在单元格内容的开头(DataGridViewTextBoxColumn, set the cursor at the beginning of the cell content)

我的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); } } }

更多推荐

本文发布于:2023-07-14 14:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1105129.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:光标   单元格   开头   内容   DataGridViewTextBoxColumn

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!