gpt4 book ai didi

c# - DataGridViewTextBoxColumn,将光标设置在单元格内容的开头

转载 作者:太空宇宙 更新时间:2023-11-03 20:27:28 25 4
gpt4 key购买 nike

我的数据网格有两个文本框列和两个按钮列。我在尝试当用户点击按钮时相邻的文本框单元格将处于编辑模式并且光标将放置在那里。

我的文本框列处于只读模式。

所以我在单元格中写了代码输入那个

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

我没有收到错误,但我的目标没有实现。文本框单元格未进入编辑模式且光标未放置在文本框单元格上。所以请告诉我并纠正我的代码,当我点击按钮时,相邻的文本框单元格将进入编辑模式,光标将在那里闪烁。

最佳答案

尝试使用 CellClick 事件。您实际上不需要调用 Focus() 只需设置 CurrentCellBeginEdit:

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

关于c# - DataGridViewTextBoxColumn,将光标设置在单元格内容的开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9719513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com