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

Can't Get Sorting to Work

2 Answers 14 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jamie
Top achievements
Rank 2
Jamie asked on 30 Oct 2013, 02:17 PM
I am using ASP.NET C# for my RadGrid and cannot get sorting to work. I have tried a couple of different ways to do it that I have found on this site, and some code from other sites, but the grid remains sorted the default way each time I try. Someone please help!

<telerik:RadGrid runat="server" ID="radProductsGrid" Skin="Default" AllowSorting="true" AllowPaging="true"
 OnSortCommand="radProductsGrid_SortCommand" OnNeedDataSource="radProductsGrid_NeedDataSource"
 AutoGenerateColumns= "false"  GridLines="None" ShowGroupPanel="false" PageSize="100">
 <MasterTableView Width="100%" AllowMultiColumnSorting="false" AllowNaturalSort="false"
  AllowCustomSorting="true" AllowSorting="true" AutoGenerateColumns="false">
  <Columns>
    <telerik:GridBoundColumn ReadOnly="true" AllowFiltering="true" SortExpression="ItemNumber"
      DataField="ItemNumber" HeaderText="Item Number" UniqueName="ItemNumber">
    </telerik:GridBoundColumn>     
    <telerik:GridBoundColumn ReadOnly="true" AllowFiltering="true" SortExpression="ProductName"
      DataField="ProductName" HeaderText="Product Name" UniqueName="ProductName">
    </telerik:GridBoundColumn>
    <telerik:GridBoundColumn ReadOnly="true" AllowFiltering="true" SortExpression="CategoryName"
      DataField="CategoryName" HeaderText="Category Name" UniqueName="CategoryName">
    </telerik:GridBoundColumn>
  </Columns>
 </MasterTableView>
</telerik:RadGrid>
 
 
 
protected void radProductsGrid_SortCommand(object sender, GridSortCommandEventArgs e)
        {
            GridTableView tableView = e.Item.OwnerTableView;
            if (e.SortExpression == "ItemNumber")
            {
                e.Canceled = true;
                GridSortExpression expression = new GridSortExpression();
                expression.FieldName = "ItemNumber";
                if (tableView.SortExpressions.Count == 0 || tableView.SortExpressions[0].FieldName
                        != "ItemNumber")
                {
                    expression.SortOrder = GridSortOrder.Descending;
                }
                else if (tableView.SortExpressions[0].SortOrder == GridSortOrder.Descending)
                {
                    expression.SortOrder = GridSortOrder.Ascending;
                }
                else if (tableView.SortExpressions[0].SortOrder == GridSortOrder.Ascending)
                {
                    expression.SortOrder = GridSortOrder.None;
                }
                tableView.SortExpressions.AddSortExpression(expression);
                tableView.Rebind();
            }
        }


2 Answers, 1 is accepted

Sort by
0
Jamie
Top achievements
Rank 2
answered on 30 Oct 2013, 02:58 PM
I found an answer on another Sort Issue in this forum. I had to remove the AllowCustomSorting="true" from my Grid and it sorts just fine now.
0
Maria Ilieva
Telerik team
answered on 04 Nov 2013, 09:54 AM
Hi Jamie,

I'm glad that you have found solution for your case. Note that in order to use any basic RadGrid feature like sorting, filtering, paging, ect. you should be sure that the custom functionality is disabled.

Regards,
Maria Ilieva
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Jamie
Top achievements
Rank 2
Answers by
Jamie
Top achievements
Rank 2
Maria Ilieva
Telerik team
Share this question
or