DataGridView - Sorting Large Datasets and Keeping Focus on Top Row

0
0

While I have found a fix for this, I am curious if there is a smoother way to implement this.

I have found that with larger datasets that end up requiring a scrollbar, when sorting a column, it “follows” the original top row.

The first image is how it looks on load.

The second is after I sort on the QA Status column. Keep in mind that there are 93 other rows with the value of “Approved” above that are not shown, as the scrollbar is no longer at the very top.

The fix I found for this is tying into the Sorted event for the DataGridView:

this.dgvLots.Sorted += new System.EventHandler(this.dgvLots_Sorted);

What is tied to a method like this:

private void dgvLots_Sorted(object sender, EventArgs e)
{
dgvLots.CurrentCell = dgvLots.Rows[0].Cells[0];
}

I saw no property on the DataGridView that seemed to correct this, or other simple fix. Am I missing something?

 

Thanks in advance.

  • You must to post comments
1
0

Hi,

you can control the behavior when sorting by setting the SortSelectionMode property.
Default is UpdatePosition so it selects the row that was selected before the sorting.
KeepPosition selects the row with the same index that was selected before.

Best regards
Frank

  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.