Sorting doesn't work for DataGrid when it's binded with a DataPager

518 views Asked by At

I have a problem with the sorting feature in a DataGrid where the paging is done by a DataPager. My DataPager control pages the data into mulitple pages as per number of rows defined for each page.

Here is my XAML code:

     <UserControl x:Class="XXXXX.ViewData"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" xmlns:dtContr="http://schemas.microsoft.com/wpf/2008/toolkit"
             xmlns:xtndrCntrl="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
             xmlns:localControls="clr-namespace:XXXXXX.Controls"
             d:DesignHeight="550" d:DesignWidth="750" Loaded="ViewData_Loaded">
    <Grid Background="#FAF9F9">
        <Grid.RowDefinitions>
            <RowDefinition Height="35"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="65"/>
        </Grid.RowDefinitions>
        <StackPanel Height="35" HorizontalAlignment="Left" Name="stackPanel1" VerticalAlignment="Top" Width="Auto" Grid.Row="0" Orientation="Horizontal" Margin="10,0">

        </StackPanel>
        <StackPanel Orientation="Vertical" Grid.Row="1">
            <dtContr:DataGrid AutoGenerateColumns="False"  HorizontalAlignment="Left" Height="340" Width="750"
                          Margin="10,10,10,0" Name="grvViewData" VerticalAlignment="Top" IsReadOnly="True" 
                          CanUserResizeColumns="True" 
                          ItemsSource="{Binding ElementName=ReportsPager,Path=CurrentPage}">

                <dtContr:DataGrid.Columns>
                    <localControls:QDDataGridColumn Header="Date" HeaderResourceID="lblDate" Width="100" Binding="{Binding Path=Date_Time}">
                    </localControls:QDDataGridColumn>
                    <localControls:QDDataGridColumn Header="Name" HeaderResourceID="lblName" Width="75" Binding="{Binding Path=Name}">
                    </localControls:QDDataGridColumn>
                </dtContr:DataGrid.Columns>
            </dtContr:DataGrid>

            <localControls:DataPager x:Name="ReportsPager" ItemsPerPage="10" Margin="0,10,10,10"
                                                 HorizontalAlignment="Right"
                                                 ItemsSource="{Binding Source={StaticResource ReportCollection}}" />


        </StackPanel>
        <Button Content="Export To Excel" Grid.Row="3" Height="35" HorizontalAlignment="Right" Margin="10,15" Name="btnExportToExcel" VerticalAlignment="Top" Width="175" Click="btnExportToExcel_Click" />
        <Button Content="Create Report" Grid.Row="3" Height="35" HorizontalAlignment="Right" Margin="10,15" Name="btnCreateReport" VerticalAlignment="Top" Width="175" Visibility="Collapsed" Click="btnCreateReport_Click"/>
    </Grid>

</UserControl>

To brief you about the above code: I have a DataGrid where I bind the ItemSource to the "ReportsPager" and registered the sorting event as well. In the next section I have a DataPager - "ReportsPager" - and the ItemSource is binded to "lstReprotView". My intention here is to prepare the lstReportveiw and the DataPager looks after the publishing the data into the DataGrid. Then when I click on one of the header columns it will be sorted automatically.

Unfortunately that does not work for me. So in the GridView.Sorting event I am explicitly sorting the "lstReprotView", but still no result.

void grvViewData_Sorting(object sender,Microsoft.Windows.Controls.DataGridSortingEventArgs e)
        {
            //Assume i sorted the lstReportView data explicitly prior to this line of code.
            ReportsPager.ItemsSource = lstReportView;

            //Validation puropose
            ObservableCollection<Object> lstReportViews = (ObservableCollection<Object>)ReportsPager.ItemsSource;
        }

Can any one please help me where I am making a mistake. With debugging, the last line while validating, I could see the sorted data, but when it's published, it's not showing the data properly. I have googled for this problem, but I have not got enough information to resolve my issue. Thanks in Advance.

Regards, Siva.

1

There are 1 answers

1
Mashton On

You could use a CollectionViewSource, like this:

<CollectionViewSource Source="{Binding lstReportView}" x:Key="ReportCollection" >
    <CollectionViewSource.SortDescriptions>
        <compMod:SortDescription PropertyName="PropertyIWantToSortOn" Direction="Ascending" />
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource>

And then bind your DataGrid (or Pager I'm guessing, not having used one) like this:

<sdk:DataGrid ItemsSource="{Binding Source={StaticResource ReportCollection}}" SelectedItem="{Binding SelectedReport, Mode=TwoWay}" etc etc>