简体   繁体   中英

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?

在此处输入图片说明

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.

Not sure if i have quite got your question but does DataGridView1.NewRowIndex not work for what you want?

I tried DataGridView1.Item(0, DataGridView1.NewRowIndex).Selected = True which seems to do what I think you are trying to achieve.

The code above is on the click event of button 1 and produces the following:

选择了新行的第一个单元格的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.

BTW I don't think you can use .CurrentCell for a cell selected on the new row, but you can use .item

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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