Use the DataGridView.FirstDisplayedScrollingRowIndex
property. It sets the index of the row that is the first row displayed on the DataGridView.
In your case since you are changing the DataSource
, new rows might have been added. You can't use the already selected row index. You have to identify the row which was selected earlier (using any unique identifier) and get its index and set the above property to this index number.
Instead of resetting the DataSource
, you could also merge the queried table with your existing table using DataTable.Merge
and refresh the grid using DataGridView.Refresh
.
Or you can fill the original DataTable again.
DataTable origTable = dataGridView1.DataSource as DataTable;
sda.Fill(origtable);
dataGridView1.Refresh();