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

后端 未结 2 2156
北恋
北恋 2020-12-19 09:13

This code

CurrentSelectedRow = Me.dgvPreviouslyCut.CurrentRow.Index

stores the current selected row that has been clicked by the user in a

2条回答
  •  忘掉有多难
    2020-12-19 09:37

    The DataGridView creates a new CurrencyManager when 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.

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

    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
    

提交回复
热议问题