Maintaining the current row in a datagrid while refreshing the tableadapter

Posted in: Forms and Grids
By dePoPo
Sep 7, 2009 - 11:04:26 AM

By default, refreshing the tableadapter on a datagridview will cause the top row to be selected after the refresh. This may be undesirable if you update data throug a subform or menu, and want the grid to maintain the same line as selected.

Here's how to make sure the active line gets reselected after the refresh;

        ' --- Save the current row on the datagrid
        Dim n_currentrow As Integer = DataGridView1.CurrentRow.Index

        ' --- refresh the grid content
        Me.SomeTableAdapter.Fill(Me.SomeDataset.SomeSelection)

        ' --- Reselect the row that was active
        DataGridView1.CurrentCell = DataGridView1.Rows(n_currentrow).Cells(1)


If you additionaly want a full row to be selected on click / return instead of a single cell being highlighted you can set the Selectionmode property of the grid to FullRowSelect (see below)

img_rowselect.jpg




Visitor Comments