vb.net 数据网格视图...以编程方式设置选择行索引不会将 CurrentRow.Index 设置为相同?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22304743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:27:31  来源:igfitidea点击:

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

vb.netuser-interfacedatagridviewindexingrows

提问by Killakeys

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.

始终设置为零,而不是您期望的变量 CurrentSelectedRow。

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

为什么以编程方式设置选择行索引不会导致属性 CurrentRow.Index 设置为相同?

回答by Junaith

CurrentRowis 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.

CurrentRow是包含当前活动单元格的行。当您将 DataGridView 绑定到外部数据源时,此属性将重置为其默认值,即第一列中的第一个单元格。

SelectedRowis the row which is currently selected/highlighted. It may be one or more rows depending on MultiSelectproperty. To select a row you have to set its Selectedproperty to true.

SelectedRow是当前选择/突出显示的行。根据MultiSelect属性,它可能是一行或多行。要选择一行,您必须将其Selected属性设置为 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 CurrentCellAddressproperty. Once your refresh the DataSourceset the CurrentCell property using these indexes.

要保留当前单元格,您必须存储当前单元格的行和列索引。为了让他们使用该CurrentCellAddress财产。刷新后,DataSource使用这些索引设置 CurrentCell 属性。

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

回答by Bj?rn-Roger Kringsj?

The DataGridViewcreates a new CurrencyManagerwhen the data source is changed. If this CM contains items, the default position is 0, thus pushing this to the DGV and selects the first row.

当数据源改变时,它DataGridView会创建一个新的CurrencyManager。如果此 CM 包含项目,则默认位置为0,因此将其推送到 DGV 并选择第一行。

To fix this, just set the position of the CM instead:

要解决此问题,只需设置 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 If