简体   繁体   中英

DataGrid data disappears when trying to sort

I have a data grid that receives its binding from a List<List<>>, the binding works well and all the data shows.

for some reason, when clicking on the header in order to sort or when rolling the scroll wheel, all the data suddenly disappears.

This is some of the code:

The grid in XAML:

<Window.Resources>
    <CollectionViewSource x:Key="ItemCollectionViewSource" CollectionViewType="ListCollectionView"/>
</Window.Resources>   

<DataGrid x:Name="DataGrid" ItemsSource="{Binding}" AutoGenerateColumns="False" DataContext="{StaticResource ItemCollectionViewSource}"  AlternatingRowBackground="LightGray" Grid.Column="1" Grid.Row="1" Height ="auto" Width="auto" FontSize="10" FontWeight="SemiBold" FontFamily="Segoe UI" Margin="5"  BorderThickness="0" Background="AliceBlue" IsReadOnly="True" VerticalScrollBarVisibility="Auto">

data binding:

CollectionViewSource itemCollectionViewSource;
itemCollectionViewSource = (CollectionViewSource)(FindResource("ItemCollectionViewSource"));
itemCollectionViewSource.Source = GetterSetter.CMinfo;

Update: I managed to solve the scroll wheel problem: need to set EnableRowVirtualization to "False"

So i Figured out what was my problem, My data grid was showing on a new window and after the window launched I cleared the data from the List I was sourcing (in order to not have duplicate rows when making a new search), instead of doing so in the beginning of the method.

It made my grid sourcing from a List that was empty (even though it showed well in the new window).

I just moved the Clear() method to the beginning of the method and now everything works well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM