简体   繁体   English

如何在行中聚焦单元格以便在DataGridView中添加新记录?

[英]How to focus cell in row for adding new record in DataGridView?

How can I programmatically focus cell in WinForms DataGridView shown highlighted on sample below? 如何在下面的示例中突出显示的WinForms DataGridView中以编程方式聚焦单元格?

在此处输入图片说明

I can easily set focus to any cell in rows 0~3 but that cell is in row 4 which looks like "virtual" because dataGridView1.Row(4) does not exist. 我可以轻松地将焦点设置到第0〜3行中的任何单元格,但是该单元格在第4行中看起来像“虚拟”,因为dataGridView1.Row(4)不存在。

Not sure if i have quite got your question but does DataGridView1.NewRowIndex not work for what you want? 不知道我是否真的有您的问题,但是DataGridView1.NewRowIndex不能满足您的需求吗?

I tried DataGridView1.Item(0, DataGridView1.NewRowIndex).Selected = True which seems to do what I think you are trying to achieve. 我尝试了DataGridView1.Item(0, DataGridView1.NewRowIndex).Selected = True ,这似乎可以实现我认为您要实现的目标。

The code above is on the click event of button 1 and produces the following: 上面的代码在按钮1的click事件上,产生以下内容:

选择了新行的第一个单元格的Datagridview

If you want to 'un-select' any currently selected cell first then precede the above with If Not IsNothing(DataGridView1.CurrentCell) Then DataGridView1.CurrentCell.Selected = False , the If Not IsNothing... is to cover the code running where no cell is currently selected. 如果要先“取消选择”当前选中的任何单元格,则在上面加上If Not IsNothing(DataGridView1.CurrentCell) Then DataGridView1.CurrentCell.Selected = FalseIf Not IsNothing...将覆盖在其中运行的代码当前未选择任何单元格。

BTW I don't think you can use .CurrentCell for a cell selected on the new row, but you can use .item 顺便说一句,我认为您不能对新行中选择的单元格使用.CurrentCell ,但可以使用.item

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM