这段代码
CurrentSelectedRow = Me.dgvPreviouslyCut.CurrentRow.Index存储用户在数据网格视图控件中单击的当前选定行。刷新数据网格视图的数据源后,此代码
Me.dgvPreviouslyCut.Rows(CurrentSelectedRow).Selected = True以编程方式重新选择同一行。
但紧接着
Me.dgvPreviouslyCut.CurrentRow.Index始终设置为零,而不是您预期的变量CurrentSelectedRow。
为什么以编程方式设置select行索引不会导致属性CurrentRow.Index设置为相同的值?
发布于 2014-03-11 00:16:00
CurrentRow是包含当前活动单元格的行。将DataGridView绑定到外部数据源时,此属性将重置为其默认值,即第一列中的第一个单元格。
SelectedRow是当前选中/突出显示的行。它可以是一行或多行,具体取决于MultiSelect属性。要选择一行,必须将其Selected属性设置为true。
通过将行设置为选中状态,您只需将其突出显示,而不会使其处于活动状态。
要保留当前单元格,必须存储当前单元格的行和列索引。使用CurrentCellAddress属性来获取它们。刷新DataSource后,使用这些索引设置CurrentCell属性。
dataGridView1.CurrentCell = dataGridView1.Rows(rowindex).Cells(columnindex);发布于 2014-03-11 00:25:53
当数据源发生更改时,DataGridView会创建一个新的CurrencyManager。如果此CM包含项目,则默认位置为0,因此将其推送到DGV并选择第一行。
要解决此问题,只需设置CM的位置:
Me.dgvPreviouslyCut.DataSource = my_new_datasource
Dim cm As CurrencyManager = CType(Me.BindingContext(my_new_datasource), CurrencyManager)
If ((Me.CurrentSelectedRow > -1) AndAlso (Me.CurrentSelectedRow < cm.Count)) Then
cm.Position = Me.CurrentSelectedRow
End Ifhttps://stackoverflow.com/questions/22304743
复制相似问题