views:

14

answers:

1

I have a DataGridView with a lot of columns which causes a horizontal scrollbar. However, when I scroll all the way to the right and sort on a column the datagridview slightly repositions itself, but the scrollbar remains all the way to the right.

I want to stop this behavior and keep the grid in the same position it was in before the grid was sorted, and I would like to continue to use Automatic sorting in the grid if possible.

I found this link and this person is having the same problem, however, the solution proposed there doesn't seem to be applicable since I am not doing manual sorting.

Any thoughts?

A: 

I solved this by doing the following.

I subscribed to the DataGridView.Scroll event, if the ScrollOrientation was Horizontal then I am setting a member variable to the NewValue of the scroll.

Then I subscribed to the DataGridView.Sorted event. In this event I set the HorizontalScrollingOffset to the member variable.


int _horizontalOffsetStop;

private void Grid_Scrolled(object sender, ScrollEventArgs e)
{
     if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
     {
          _horizontalOffsetStop = e.NewValue;
     }
}

private void Grid_Sorted(object sender, EventArgs e)
{
     myGrid.HorizontalScrollingOffset = _horizontalOffsetStop;
}

Adam