数据网格视图...以编程方式设置选择行索引没有将CurrentRow.Index设置为相同? [英] Data Grid View...programmatically setting the select row index doesn't set the CurrentRow.Index to the same?
问题描述
此代码
CurrentSelectedRow = Me.dgvPreviouslyCut.CurrentRow.Index
/ pre>
将用户点击的当前所选行存储在数据网格视图控件中。
刷新数据网格视图的数据源后,此代码Me.dgvPreviouslyCut.Rows(CurrentSelectedRow).Selected = True
以编程方式重新选择同一行。
不久之后,
Me.dgvPreviouslyCut.CurrentRow.Index
/ pre>
始终设置为零,而不是您期望的变量CurrentSelectedRow。
为什么以编程方式设置选择行索引不会导致属性CurrentRow.Index设置为相同?
解决方案
CurrentRow
是包含当前活动单元格的行。将DataGridView绑定到外部数据源时,此属性将重置为默认值,该值是第一列中的第一个单元格。
SelectedRow
是当前选中/突出显示的行。它可能是一个或多个行,具体取决于MultiSelect
属性。要选择一行,您必须将其所选
属性设置为true。
通过将行设置为选定,您只需保持它突出显示不使其活动。
要保留当前单元格,您必须存储当前单元格的行和列索引。要让他们使用
CurrentCellAddress
属性。刷新DataSource
后,使用这些索引设置CurrentCell属性。dataGridView1.CurrentCell = dataGridView1.Rows(rowindex).Cells(columnindex);
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?
解决方案
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 onMultiSelect
property. To select a row you have to set itsSelected
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 theDataSource
set the CurrentCell property using these indexes.dataGridView1.CurrentCell = dataGridView1.Rows(rowindex).Cells(columnindex);
这篇关于数据网格视图...以编程方式设置选择行索引没有将CurrentRow.Index设置为相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!