0

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.

siva Rapolu
  • 419
  • 1
  • 8
  • 20
  • What type has `lstReportView` before the sorting? – Herdo Dec 12 '13 at 09:49
  • Its List. Here is the code for that.foreach (var obj in lstReportView) { ReportData.Add(obj); } ReportsPager.ItemsSource = ReportData; – siva Rapolu Dec 12 '13 at 10:02
  • Do you use MVVM pattern? If so, there should be no need to set the `ReportsPager.ItemSource` manually. – Herdo Dec 12 '13 at 10:20
  • I am not using MVVM pattern. Does this give the side effect of sorting and all. Implementing MVVM solves the problem? I agree MVVM is storngly recommended and good practice to use. – siva Rapolu Dec 12 '13 at 10:36
  • Yes, MVVM would be a better approach here. But I'm not sure about how good the pattern will work with the `DataPager`. – Herdo Dec 12 '13 at 12:53

1 Answers1

0

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>
Mashton
  • 5,069
  • 1
  • 22
  • 32
  • Hi Mashton, Thanks for suggesting me to use CollectionViewSource. Thats the approach i need to follow for sorting. But in my XAML, i could not able to add the collection viewsource under Datapager control. I am adding my XAML in my post. Can you please help me in adding this. I am getting compile time errors while adding this. – siva Rapolu Dec 16 '13 at 12:27