首页 新闻 赞助 找找看

DataGridView某个单元格得到焦点

1
悬赏园豆:10 [待解决问题]

在winform项目中,ShowDialog出某个窗体,在这个窗体中第一行第二列的单无格默认获得焦点,如何实现

ssh800的主页 ssh800 | 初学一级 | 园豆:17
提问于:2014-12-08 13:37
< >
分享
所有回答(3)
0

this.dataGridView1.CurrentCell = this.dataGridView1.Rows[4].Cells[1]; //自己改成第几行第几列。 
this.dataGridView1.BeginEdit(true); 

问天何必 | 园豆:3311 (老鸟四级) | 2014-12-08 13:47

不行,这个方法我式过

支持(0) 反对(1) ssh800 | 园豆:17 (初学一级) | 2014-12-08 13:56

@ssh800: dataGridView1.Rows[0].Cells[1].Selected = true;

自己再试试, 我也忘了。 

支持(0) 反对(0) 问天何必 | 园豆:3311 (老鸟四级) | 2014-12-08 14:08

@问天何必: 这些方法都试过了,不行

支持(0) 反对(0) ssh800 | 园豆:17 (初学一级) | 2014-12-08 18:56
1

 

貌似dataGridView.CurrentCell设置太早了不起作用,可能它自己内部函数运行时给覆盖了,所有在设置current cell时要足够晚。我采取的解决方法是在CellFormatting 事件的回调中设置,验证可行。

代码:

1.首先要订阅事件,代码要放在给dataGridView.DataSource赋值之前

1   this.dataGridView.CellFormatting += (sender, e) => OnCellFormatting(e);

2.实现选中效果,需要添加一个flag,或者会影响正常的编辑

 1 private bool m_HasGridInitialized =false;
 2 
 3 private void OnCellFormatting(DataGridViewCellFormattingEventArgs e)
 4 {
 5     if (!m_HasGridInitialized &&  e.RowIndex == 0 && e.ColumnIndex == 1)
 6     {
 7       dataGridView.CurrentCell = dataGridView[e.ColumnIndex, e.RowIndex];
 8        m_HasGridInitialized = true;
 9    }
10 }

 

leowork | 园豆:202 (菜鸟二级) | 2016-01-24 00:20
0

感谢 leowork的回答,帮助我解决了问题

张三的歌丶 | 园豆:202 (菜鸟二级) | 2017-02-09 13:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册