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

Sorting groups numerically

2 Answers 151 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Deborah Blackwell
Top achievements
Rank 1
Deborah Blackwell asked on 09 Jan 2014, 07:44 PM
I'm working on a grid that implements the grouping functionality and we are having an issue when attempting to sort the groups ascending or descending.  The grouping field is an item number and needs to be sorting by value, however we can't seem to get it to not sort alphabetically (Item 10 always comes before Item 2).  The associated column is an integer, so ideally it should sort numerically.

Is it possible to sort by just the group's value and not the formatted string?  We currently have it formatted as "Item #", however we can strip any extra characters if necessary.

<telerik:RadGrid SkinID="Paging" ID="ResultsGrid" runat="server" AllowCustomPaging = "True" AllowPaging="False"
            OnInit="DataGridInit" OnItemDataBound="BindColumnValues"
            OnNeedDataSource="DataGrid_NeedDataSource"
            OnItemCreated="ItemManipulation"
            OnSortCommand="PerformSort"
            OnItemCommand="ResultsGridItemCommand" ShowGroupPanel="true" AutoGenerateColumns="false"
        GridLines="none" ShowFooter="false" EnableLinqExpressions="false">
            <PagerStyle Mode="NextPrevNumericAndAdvanced"></PagerStyle>
            <GroupingSettings ExpandTooltip="Expand" CollapseTooltip="Collapse"  />
            <MasterTableView Summary="<%$ Resources: ResultsTableSummary %>" ShowGroupFooter="true"
                ClientDataKeyNames=""
                DataKeyNames="ItemDocumentKey,FundingKey" CssClass="rgWrapFalse">
                  
                <Columns>
                    <telerik:GridBoundColumn UniqueName="ItemNumber" DataField="ItemNumber" HeaderText="<%$
Resources: PostAwardCommon, ItemNumber %>" DataType="System.Int32"
                        Visible="false"/>                           
                    <telerik:GridBoundColumn UniqueName="ShipToCode" DataField="ShipToCode" HeaderText="<%$ Resources: Common, DeliverTo %>" DataType="System.String"
                        SortExpression="ShipToCode" />
                    <telerik:GridBoundColumn DataField="AccountId"
                        HeaderText="<%$ Resources: PostAwardCommon, AccountId %>" UniqueName="AccountId" AllowSorting="true"/>
                    <telerik:GridBoundColumn DataField="AccountCode"
                        HeaderText="<%$ Resources: PostAwardCommon, AccountingCode %>" UniqueName="AccountCode" AllowSorting="true"/>
                    <telerik:GridBoundColumn DataField="AwardedAmount" HeaderText="<%$ Resources: PostAwardCommon, Awarded %>" AllowSorting="true"
                    UniqueName="AwardedAmount" HeaderStyle-CssClass="rgHeader width30 rgHeaderAlignLeft" ItemStyle-HorizontalAlign="Right"/>
                    <telerik:GridBoundColumn DataField="AcceptedToDate" HeaderText="<%$ Resources: PostAwardCommon, AcceptedtoDateHeaderText %>" AllowSorting="true"
                    UniqueName="AcceptedToDate" HeaderStyle-CssClass="rgHeader width45 rgHeaderAlignLeft" ItemStyle-HorizontalAlign="Right"/>
                    <telerik:GridBoundColumn DataField="RemainingAmount" HeaderText="<%$ Resources: PostAwardCommon, Remaining %>" AllowSorting="true"
                    UniqueName="RemainingAmount" HeaderStyle-CssClass="rgHeader width30 rgHeaderAlignLeft" ItemStyle-HorizontalAlign="Right"/>                                                          
                    <telerik:GridBoundColumn UniqueName="AcceptedDate" HeaderText="<%$ Resources: PostAwardCommon, ActualAcceptedDate %>"
                    ItemStyle-HorizontalAlign="Center" />                  
                    <telerik:GridBoundColumn UniqueName="AcceptedAmount"  FooterStyle-HorizontalAlign="Left" FooterText="Total Accepted: "
                    GroupByExpression="AcceptedAmount Group By AcceptedAmount" DataType="System.Double"  Aggregate="Sum" DataField="AcceptedAmount"
                    ItemStyle-HorizontalAlign="Right"/>                                     
                </Columns>
                  
                <GroupByExpressions>
                    <telerik:GridGroupByExpression>
                        <SelectFields>
                            <telerik:GridGroupByField FieldAlias="ItemNumber" HeaderText="<%$ Resources: PostAwardCommon, ItemNumber %>" FieldName="ItemNumber"></telerik:GridGroupByField>
                        </SelectFields>
                        <GroupByFields>
                            <telerik:GridGroupByField FieldName="ItemNumber" SortOrder="Ascending"></telerik:GridGroupByField>
                        </GroupByFields>
                    </telerik:GridGroupByExpression>
                </GroupByExpressions>               
                  
            </MasterTableView>
 
        </telerik:RadGrid>

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 10 Jan 2014, 09:24 AM
Hi Deborah Blackwell,

I tried your code and was not able to identify the issue. Below is a sample code i tried, and it sorted the number column which is grouped and not alphabetically. Please try and let me know.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
    AllowSorting="true" AllowMultiRowSelection="true" OnNeedDataSource="RadGrid1_NeedDataSource">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn UniqueName="ID" DataField="ID" HeaderText="ID" />
            <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name" />
            <telerik:GridBoundColumn DataField="Number" HeaderText="Number" UniqueName="Number"
                DataFormatString="Item {0}" />
        </Columns>
        <GroupByExpressions>
            <telerik:GridGroupByExpression>
                <SelectFields>
                    <telerik:GridGroupByField FieldAlias="Number" HeaderText="Number" FieldName="Number">
                    </telerik:GridGroupByField>
                </SelectFields>
                <GroupByFields>
                    <telerik:GridGroupByField FieldName="Number" SortOrder="Ascending"></telerik:GridGroupByField>
                </GroupByFields>
            </telerik:GridGroupByExpression>
        </GroupByExpressions>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
  dynamic data = new[] {
  new { ID = 1, Name = "Name1", Number=1},
  new { ID = 2, Name = "Name2", Number=2},
  new { ID = 3, Name = "Name3", Number=2},
  new { ID = 4, Name = "Name4", Number=4},
  new { ID = 5, Name = "Name5", Number=5},
  new { ID = 6, Name = "Name6", Number=5},
  new { ID = 7, Name = "Name7", Number=7},
  new { ID = 8, Name = "Name8", Number=8},
  new { ID = 9, Name = "Name9", Number=7}
  };
 RadGrid1.DataSource = data;
}

Thanks,
Princy

0
Deborah Blackwell
Top achievements
Rank 1
answered on 10 Jan 2014, 06:51 PM
We've solved the issue.  In the code behind item number was a string, and being cast to an integer when put in the grid through the column's datatype property.  We had assumed that the sorting was referring to the column's data type, but it seems it is referring directly to the object as changing the datatype in the code behind did the trick.  Funny enough, we had thought we already tried this, but we have two similar objects with item number and we tested changing the data type in the wrong one.
Tags
Grid
Asked by
Deborah Blackwell
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Deborah Blackwell
Top achievements
Rank 1
Share this question
or