This is a migrated thread and some comments may be shown as answers.

Customize Group and Sort

9 Answers 318 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Betsy
Top achievements
Rank 1
Betsy asked on 02 Aug 2012, 08:43 PM
WPF 4.0 v2012.2.607.35
I have a grid of charges that are on invoices.  I want to show the charges by most recent invoice but sorting on the invoice number sorts incorrectly because it is a text field.  What I need to do is group by and show the InvoiceNumber but sort by the InvoiceId descending and not have the InvoiceId shown anywhere. 

9 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 03 Aug 2012, 12:01 PM
Hello,

 Indeed the text fields will be sorted as string. You can set SortMemberPath="InvoiceId" for the "InvoiceNumber" column. Then the sorting will be done on the "InvoiceId" column.

All the best,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Betsy
Top achievements
Rank 1
answered on 03 Aug 2012, 01:48 PM
That does not seem to work consistently with grouping.  If I apply the SortMemberPath without grouping it works perfectly.  If I sort the column by clicking on it then drag the column to the group header it seems to loose the sort.  If I then click on the column header it sorts correctly but if I click on the field in the group by panel it sorts alphanumerically.  And I can't get the sort to default correctly at all if I try to define it in XAML.
<ctrls:BPGridView Grid.Row="0" AutoGenerateColumns="False" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem,Mode=TwoWay}" AutoExpandGroups="True">
    <telerik:RadGridView.GroupDescriptors>
      <telerik:GroupDescriptor Member="InvoiceNumber" DisplayContent="Invoice" SortDirection="Descending" />
    </telerik:RadGridView.GroupDescriptors>
   <telerik:RadGridView.Columns>
    <telerik:GridViewBoundColumnBase Header="Description" DataMemberBinding="{Binding ChargeDescription}" ShowDistinctFilters="False" >
      <telerik:GridViewBoundColumnBase.CellTemplate>
        <DataTemplate>
          <TextBlock>
              <Hyperlink Command="{Binding DataContext.SelectCommand, RelativeSource={RelativeSource AncestorType={x:Type telerik:RadGridView}}}" CommandParameter="{Binding}" Style="{StaticResource BPHyperlinkStyle}">
                    <TextBlock Text="{Binding ChargeDescription}"/>
              </Hyperlink>
            </TextBlock>
        </DataTemplate>
      </telerik:GridViewBoundColumnBase.CellTemplate>
      <telerik:GridViewBoundColumnBase.AggregateFunctions>
        <telerik:CountFunction Caption="Count:  " />
      </telerik:GridViewBoundColumnBase.AggregateFunctions>
    </telerik:GridViewBoundColumnBase>
    <telerik:GridViewDataColumn Header="Amount" DataMemberBinding="{Binding ChargeTotal, StringFormat=\{0:c\}}" FooterCellStyle="{StaticResource GridNoLinesFooterStyle}" ShowDistinctFilters="False"/>
    <telerik:GridViewDataColumn Header="Service" DataMemberBinding="{Binding ServiceOfferingName}" FooterCellStyle="{StaticResource GridNoLinesFooterStyle}" ShowDistinctFilters="True"/>
    <telerik:GridViewDataColumn Header="Effective Date" DataMemberBinding="{Binding Path=EffectiveDate, StringFormat=\{0:d\}}" FooterCellStyle="{StaticResource GridNoLinesFooterStyle}" />
    <telerik:GridViewDataColumn Header="Type" DataMemberBinding="{Binding Path=ChargeTypeName}" FooterCellStyle="{StaticResource GridNoLinesFooterStyle}"/>
    <telerik:GridViewDataColumn x:Name="invcol" Header="Invoice" SortMemberPath="InvoiceIdSort" DataMemberBinding="{Binding Path=InvoiceNumber,TargetNullValue=None}" FooterCellStyle="{StaticResource GridNoLinesFooterStyle}"/>
    <telerik:GridViewDataColumn Header="Date Created" DataMemberBinding="{Binding Path=DateCreated, StringFormat=\{0:d\}}" FooterCellStyle="{StaticResource GridNoLinesFooterStyle}" />
    <telerik:GridViewDataColumn Header="Invoiced" DataMemberBinding="{Binding Path=InvoicedFlag}" FooterCellStyle="{StaticResource GridNoLinesFooterStyle}" />
  </telerik:RadGridView.Columns>
</ctrls:BPGridView>
0
Pavel Pavlov
Telerik team
answered on 06 Aug 2012, 11:43 AM
Hi Heather,

You may adjust the resulting sort state by handling the grouping event of RadGridView. Inside the event handler you may set the desired sorting state.

I case you need a sample on this , please paste the implementation of your Invoice objects and I will gather a small runnable project  for you.

All the best,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Betsy
Top achievements
Rank 1
answered on 08 Aug 2012, 01:35 PM
Even if I adjust it in the Grouping event it still ignores the SortMemberPath when the column is being grouped by.  If the column is not being grouped by the SortMemberPath works correctly, if the column is being grouped by then the sort goes back to being alphanumeric based on the value in the InvoiceNumber field.

Example Data:
InvoiceIdSort=10  InvoiceNumber="0412"
InvoiceIdSort=9  InvoiceNumber = "0312"
InvoiceIdSort=8 InvoiceNumber="0212"
InvoiceIdSort=7  InvoiceNumber="0411"
InvoiceIdSort=6 InvoiceNumber="0311"
InvoiceIdSort=5  InvoiceNumber="0211"

Using the XAML from earlier posting if you remove the grouping then the data is correctly sorted by the InvoiceIdSort value,  If I then group by the InvoiceNumber column, the sort uses the InvoiceNumber field value NOT the InvoiceIdSort field value.
0
Dimitrina
Telerik team
answered on 13 Aug 2012, 02:33 PM
Hello,

Thank you for that additional information. We will need a little bit more time to check the problem. I will write back to you with more relative information tomorrow.

Thank you for your patience.

Kind regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Dimitrina
Telerik team
answered on 15 Aug 2012, 06:22 AM
Hello,

You can sort the grouped InvoiceNumber column based on the value of the InvoiceIdSort like so:

private void clubsGrid_Grouping(object sender, GridViewGroupingEventArgs e)
{
           if (e.Action == GroupingEventAction.Place)
           {
               e.Cancel = true;
               var descriptor = new GroupDescriptor<Club, string, int>
               {
                   GroupingExpression = i => i.InvoiceNumber,
                   GroupSortingExpression = i => i.FirstOrDefault().InvoiceIdSort
               };
               descriptor.DisplayContent = ((Telerik.Windows.Data.GroupDescriptorBase)(e.GroupDescriptor)).DisplayContent;
               descriptor.SortDirection = e.GroupDescriptor.SortDirection;
               this.clubsGrid.GroupDescriptors.Add(descriptor);
                
           }
}

In my code snippet the bound object  is of type Club.

Kind regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
gisela
Top achievements
Rank 1
answered on 10 Apr 2013, 01:15 PM
Hi all,

your code works fine, except the AggregateFunctions (of another colums) are not executed and the GroupFooter is empty. How can i achieve, that this works.

Thanks in advance
0
Chaitanya
Top achievements
Rank 1
answered on 21 Jan 2016, 01:06 PM

Hi Team,

 var descriptor = new GroupDescriptor<Club, string, int>
               {
                   GroupingExpression = i => i.InvoiceNumber,
                   GroupSortingExpression = i => i.FirstOrDefault().InvoiceIdSort
               };

When I am using this, data is been grouped properly but when I try to sort by clicking on grouped element from group header. it is giving System.Reflection.TargetInvocationException, can you please help me.

0
Dilyan Traykov
Telerik team
answered on 25 Jan 2016, 12:33 PM
Hello Chaitanya,

As I am unable to reproduce the exception, could you check if there are any inner exceptions and if so, post them here so that we can have a look? Could you also please provide more information about the application you've set up? some code snippets would definitely be of help.

If you prefer, you can open a new support ticket where you could send us a sample project, demonstrating the exception.

Regards,
Dilyan Traykov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
Betsy
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Betsy
Top achievements
Rank 1
Pavel Pavlov
Telerik team
gisela
Top achievements
Rank 1
Chaitanya
Top achievements
Rank 1
Dilyan Traykov
Telerik team
Share this question
or