Reputation: 51
I have a small WPF application with a DataGrid
. When I update my dataSource
like this:
DataTable myTable = GetNewTableWithChangedRows();
MyGrid.ItemsSource = myTable.DefaultView;
The vertical scroll bar stays on the user defined position, but the horizontal scrollbar resets its position to zero.
Where is the difference? And how can I make the horizontal scrollbar stay on the user defined position?
Upvotes: 1
Views: 773
Reputation: 15197
This occurs, because the DataGrid
generates all columns anew on changing the items source. The column widths will be calculated again, so the scroll viewer will reset its horizontal position.
The vertical position (and the row selection) will be maintained by design: the Selector
will try to maintain the current selection.
If you want to keep the horizontal scroll position unchanged, you have to prevent the automatic column generation. That means, you have to define the DataGrid
columns manually:
<DataGrid AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Header 1" Binding="{Binding Column1Value}"/>
<DataGridTextColumn Header="Header 2" Binding="{Binding Column2Value}"/>
<!-- ...and so on, for each column you need -->
</DataGrid.Columns>
</DataGrid>
Upvotes: 2