Fresh
Fresh

Reputation: 183

Reset DataGrid Sorting back to its initial state

I have created a view that contains some buttons and a DataGrid. The DataGrid displays items as the application parses an excel file. I have added a button that cancels the parsing thread. I am able to clear the DataGrid the only issue I am having is if any of the columns have been sorted that sort persists through the cancel.

Heres an example of my Datagrid:

<DataGrid ItemsSource="{Binding Parts}"
                  Visibility="{Binding DatagridIsVisible, Converter={StaticResource BooleanToVisibilityInverseParameterConverter}}"
                  AutoGenerateColumns="False" 
                  Margin="0,0,0,44" 
                  FontSize="20" 
                  AlternationCount="2"
                  AlternatingRowBackground="WhiteSmoke"
                  CanUserAddRows="False">
  <DataGrid.Columns >
    <DataGridTextColumn  Header="Raw Data" 
                         Binding="{Binding RawData}">
        <DataGridTextColumn.CellStyle>
            <Style>
                <Setter Property="Border.BorderBrush" Value="Black"/>
                <Setter Property="Border.BorderThickness" Value="0 0 1 0"/>
            </Style>
        </DataGridTextColumn.CellStyle>
    </DataGridTextColumn>   
    <DataGridTextColumn  Header="Manufacturer" Binding="{Binding Manu}">
        <DataGridTextColumn.CellStyle>
            <Style>
                <Setter Property="Border.BorderBrush" Value="Black"/>
                <Setter Property="Border.BorderThickness" Value="0 0 1 0"/>
            </Style>
        </DataGridTextColumn.CellStyle>
    </DataGridTextColumn>       
  </DataGrid.Columns>
</DataGrid>

I have combed through the internet and have found examples on how to do this using the code behind method but I am really trying to stick to MVVM for this.

Is this even possible?

Upvotes: 1

Views: 1020

Answers (1)

ganchito55
ganchito55

Reputation: 3607

I think that you should use a Collection View instead of an Observable Collection for your ItemsSource, because if you use an Observable Collection you only have a Collection View so all changes are kept. However if you use a Collection View you can change the collection view and remove the sorting.

You can see more here

I hope that this can help you

Upvotes: 1

Related Questions