Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

vb.net - Data Grid View...programmatically setting the select row index doesn't set the CurrentRow.Index to the same?

This code

CurrentSelectedRow = Me.dgvPreviouslyCut.CurrentRow.Index

stores the current selected row that has been clicked by the user in a data grid view control . After refreshing the datasource for the data grid view, this code

Me.dgvPreviouslyCut.Rows(CurrentSelectedRow).Selected = True

programmatically re-selects the same row.

Immediately afterwards though

 Me.dgvPreviouslyCut.CurrentRow.Index

is always set to zero, NOT the variable CurrentSelectedRow as you would expect.

Why does programmatically setting the select row index not cause the property CurrentRow.Index to be set to the same?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

CurrentRow is the row containing the currently active cell. When you bind the DataGridView to an external data source, this property is reset to its default value which is the first cell in the first column.

SelectedRow is the row which is currently selected/highlighted. It may be one or more rows depending on MultiSelect property. To select a row you have to set its Selected property to true.

By setting the row as selected you merely keeping it highlighted not making it active.

To retain the current cell you have to store the Current cell's row and column index. To get them use the CurrentCellAddress property. Once your refresh the DataSource set the CurrentCell property using these indexes.

dataGridView1.CurrentCell = dataGridView1.Rows(rowindex).Cells(columnindex);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...