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

RadGrid Sorting

16 Answers 1261 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Deyan
Top achievements
Rank 1
Deyan asked on 25 Oct 2010, 02:02 PM

I want simple thing to do when I click to sort any column:

first click DESC , second ASC, third NONE 

not like now- first click ASC , second DESC, third NONE 

It will be very easy but the e.NewSortOrder is read-only :(

16 Answers, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 25 Oct 2010, 02:31 PM
Hi Deyan,

Please refer to the Controlling sorting modes help topic, which elaborates on how to change the way the sort mode changes when the user clicks on the sort button in a column header.

All the best,
Pavlina
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Deyan
Top achievements
Rank 1
answered on 25 Oct 2010, 03:14 PM

It work after some modefication

01.protected void gvBA_SortCommand(object source, GridSortCommandEventArgs e)
02.   {
03.           e.Canceled = true;
04.           GridSortExpression expression = new GridSortExpression();
05.           expression.FieldName = e.SortExpression;
06.           if (gvBA.MasterTableView.SortExpressions.Count == 0 ||
07.               gvBA.MasterTableView.SortExpressions[0].FieldName != e.SortExpression)
08.           {
09.               expression.SortOrder = GridSortOrder.Descending;
10.           }
11.           else if (gvBA.MasterTableView.SortExpressions[0].SortOrder == GridSortOrder.Descending)
12.           {
13.               expression.SortOrder = GridSortOrder.Ascending;
14.           }
15.           else if (gvBA.MasterTableView.SortExpressions[0].SortOrder == GridSortOrder.Ascending)
16.           {
17.               expression.SortOrder = GridSortOrder.None;
18.           }
19.           else if (gvBA.MasterTableView.SortExpressions[0].SortOrder == GridSortOrder.None)
20.           {
21.               expression.SortOrder = GridSortOrder.Descending;
22.           }
23.           gvBA.MasterTableView.SortExpressions.AddSortExpression(expression);
24.           gvBA.MasterTableView.Rebind();
25.   }
But I think this is very complex for such simple thing...

All the best.

0
Pavlina
Telerik team
answered on 27 Oct 2010, 11:12 AM
Hello Deyan,

Another option to skip this default sort behavior and provide your own custom sort order for a column is by setting the AllowCustomSorting property of the corresponding table view. Review the help article below for more information:
http://www.telerik.com/help/aspnet-ajax/grdapplycustomsortcriteria.html

If you need further assistance, do not hesitate to let us know.

Kind regards,
Pavlina
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
andrea
Top achievements
Rank 1
answered on 12 Jan 2011, 04:57 AM
Hi, 
I would like to know if there is some way to support custom and default sorting on the same grid (some columns with custom sorting and the others stay with default). 

Thanks,
0
Pavlina
Telerik team
answered on 14 Jan 2011, 05:59 PM
Hello Deyan,

With custom sorting turned on, RadGrid will display the sorting icons but it will not actually sort the data. You will need to perform the custom sorting manually inside the SortCommand event handler.

Regards,
Pavlina
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Waseem
Top achievements
Rank 1
answered on 07 Dec 2012, 01:42 PM
I have Total row at bottom of RadGrid. when i am trying to sort it . it come above . i don't want to sort last row. Is there any way to sort radgrid except last row of RadGrid. which contain total. (By default my Total count row is at bottom.)

0
Pavlina
Telerik team
answered on 12 Dec 2012, 01:57 PM
Hi,

Did you check the below help article pointed in one of the previous posts? It demonstrates how you can apply your own logic for sorting:
http://www.telerik.com/help/aspnet-ajax/grid-apply-custom-sort-criteria.html

All the best,
Pavlina
the Telerik team
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 their blog feed now.
0
Waseem
Top achievements
Rank 1
answered on 13 Dec 2012, 08:31 PM
Thanks Pavlina
         
you save my life. Its works great 100%.

Thanks,
Muhammad Waseem


0
Robert
Top achievements
Rank 1
answered on 22 Jan 2013, 09:17 PM
Hi Pavlina, you seem to be the sorting expert so I'll ask my question here especially since it relates to custom sorting. I used the example to create my custom sorting and the sorting works fine. My issue is that I can't seem to figure out how to set the initial grid sorting state so it renders the sort header icons correctly. So for example when I first open the page my table is sorted on Total_Cost ASC but I can't figure out a way to put that into the grid sorting state. When I did non-custom sorting this code in Page_Load worked but it no longer works.
GridSortExpression gridSortExpression = new GridSortExpression();
gridSortExpression.SortOrder = GridSortOrder.Ascending;
gridSortExpression.FieldName = QuoteEngineFields.TotalBaseCost;
PlanGrid.MasterTableView.SortExpressions.AddSortExpression(gridSortExpression);
I think this is causing another issue where I have to click the sort header twice in order for it to sort correctly. The first time I click it, the OnSortCommand doesn't fire. The second time it fires correctly.

Here is the pertinent grid sort configuration.

<telerik:RadGrid ID="PlanGrid" runat="server" EnableEmbeddedSkins="false" Width="100%"
    OnNeedDataSource="PlanGrid_NeedDataSource" OnDetailTableDataBind="PlanGrid_OnDetailTableDataBind"
    OnItemDataBound="PlanGrid_ItemDataBound" OnItemCommand="PlanGrid_ItemCommand"
    OnSortCommand="PlanGrid_OnSortCommand"
    AllowToggleByRow="true" AllowSorting="true" AutoGenerateColumns="false" DataKeyField="PID"
    ClientIDMode="Static" EnableLinqExpressions="false" ClientSettings-Scrolling-UseStaticHeaders="true">
    <ValidationSettings EnableValidation="false" ValidationGroup="PlanGridValidationGroup" />
    <MasterTableView EnableLinqGrouping="false" EnableColumnsViewState="false" AllowMultiColumnSorting="false" AllowNaturalSort="false" AllowCustomSorting="true"
        DataKeyNames="PID,Carrier_ID,Carrier_State_ID,Total_Cost" Name="PlanList" DataMember="Plans">

Thanks!

/Rob
0
Shinu
Top achievements
Rank 2
answered on 23 Jan 2013, 11:37 AM
Hi Robert,

Please try the following code snippet.

C#:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        PlanGrid.MasterTableView.AllowCustomSorting = false;
        GridSortExpression gridSortExpression = new GridSortExpression();
        gridSortExpression.SortOrder = GridSortOrder.Ascending;
        gridSortExpression.FieldName = QuoteEngineFields.TotalBaseCost;
        PlanGrid.MasterTableView.SortExpressions.AddSortExpression(gridSortExpression);
    }
}

Thanks,
Shinu.
0
Robert
Top achievements
Rank 1
answered on 24 Jan 2013, 03:30 PM
Hi Shinu. That actually doesn't work because I need custom sorting so I can't turn it off. My question is, given that I am using custom sorting and given the page when it first opens the sorting is total cost ascending, is there a way to set the grid to the headers display the sort icons correctly? And so that internally the grid starts tracking the sort state correctly? I know I can force set the icon in the OnItemCreated event but the grid won't "know" internally the sort state.

Thanks!
0
Pavlina
Telerik team
answered on 27 Jan 2013, 01:25 PM
Hi,

I am not sure if I understand what is the exact problem you are facing. Basically, when custom sorting is enabled RadGrid displays the sorting icons but does not actually sort the data.  The only thing you should do is to perform the custom sorting inside the SortCommand event handler.

Greetings,
Pavlina
the Telerik team
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 their blog feed now.
0
Pavlina
Telerik team
answered on 27 Jan 2013, 01:25 PM
Hi,

I am not sure if I understand what is the exact problem you are facing. Basically, when custom sorting is enabled RadGrid displays the sorting icons but does not actually sort the data.  The only thing you should do is to perform the custom sorting inside the SortCommand event handler.

Greetings,
Pavlina
the Telerik team
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 their blog feed now.
0
Robert
Top achievements
Rank 1
answered on 05 Feb 2013, 07:48 PM
Hi Pavlina,

Sorry about the delay it took a few days to find the time to build a simple example. My issue is that the grid doesn't seem to be handling the icons correctly when I apply custom sorts. Our requirement is that we want to apply "post-processing" after performing the requested sort. So for example when our user thinks they are sorting by Total Cost ASC we do the following:

1) Sort by Total Cost ASC
2) Update the SortOrder data column with this order
3) apply custom sort logic which updates SortOrder for some rows
4) display the grid based on sorting SortOrder

In my example below when the page loads it is correctly sorting by Total Cost. Click total cost again. Notice how the actual grid order hasn't changed but the icon in the header has changed. This happens because even though the icon is set correctly, when you click Total Cost it passes the PreviousSort as "None" so it sets the actual sort to Total Cost ASC. The grid state doesn't think there was an original sort when the page loaded even though the icon was drawn correctly. If you click Total Cost a second time you see that the grid items do change order but now the icon is wrong.

Thanks!

/Rob

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPlaceholder.master" AutoEventWireup="true" CodeFile="TelerikTest.aspx.cs" Inherits="IFP_Quoting_TelerikTest" %>
 
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<%@ Register Assembly="HealthConnect.Web.WebControls3" Namespace="HealthConnect.Web.WebControls3" TagPrefix="hc" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
 
<telerik:RadScriptManager ID="RadScriptManager1" runat="server" />
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
	<AjaxSettings>
		<telerik:AjaxSetting AjaxControlID="PlanGrid">
			<UpdatedControls>
				<telerik:AjaxUpdatedControl ControlID="PlanGrid" />
			</UpdatedControls>
		</telerik:AjaxSetting>
	</AjaxSettings>
</telerik:RadAjaxManager>
 
 
<table class="pwTableDetail" style="width890px;" cellpadding="0" cellspacing="0"
    border="0">
    <tr>
        <td>
            <hc:ValidationSummary ID="validationSummary" runat="server" DisplayMode="BulletList" ShowSummary="true" Visible="true" CssClass="ContentErrors" />
        </td>
    </tr>
    <tr>
        <td>
            <table id="tblPlanGrid" cellpadding="0" cellspacing="0" class="gridWrapper" runat="server">
                <tr>
                    <td>
                        <telerik:RadGrid ID="PlanGrid" runat="server" EnableEmbeddedSkins="false" Width="100%"
                            OnNeedDataSource="PlanGrid_NeedDataSource" OnSortCommand="PlanGrid_OnSortCommand"
                            AllowToggleByRow="true" AllowSorting="true" AutoGenerateColumns="false" DataKeyField="PID"
                            ClientIDMode="Static" EnableLinqExpressions="false" ClientSettings-Scrolling-UseStaticHeaders="true">
                            <ValidationSettings EnableValidation="false" ValidationGroup="PlanGridValidationGroup" />
                            <MasterTableView EnableLinqGrouping="false" EnableColumnsViewState="false" AllowMultiColumnSorting="false" AllowNaturalSort="false" AllowCustomSorting="true"
                                DataKeyNames="PID" Name="PlanList" DataMember="Plans">
                                <NoRecordsTemplate>We could not find any plans based on your criteria.</NoRecordsTemplate>
                                <SortExpressions>
                                    <telerik:GridSortExpression FieldName="Total_Cost" SortOrder="Ascending" />
                                </SortExpressions>
                                <Columns>
                                    <telerik:GridBoundColumn DataField="Plan_Name" SortExpression= "Plan_Name" HeaderText="Plan Name" />
                                    <telerik:GridBoundColumn DataField="Total_Total_Cost" SortExpression= "Total_Cost" HeaderText="Total Cost" />
                                </Columns>
                            </MasterTableView>
                        </telerik:RadGrid>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
</asp:Content>

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml;
 
 
using Microsoft.ApplicationBlocks.ExceptionManagement;
using Telerik.Web.UI;
 
 
public partial class IFP_Quoting_TelerikTest : HealthConnect.Web.AMS.AMSPage
{
 
    protected void Page_Load(object sender, EventArgs e)
    {
        //Set the skin, in this case the name the skin is similar to the name of the theme
        this.PlanGrid.Skin = this.Page.Theme;
    }
 
    private DataRow[] loadData()
    {
 
        #region Fake populate the dataset
 
        DataSet plans = new DataSet();
 
        plans.Tables.Add(new DataTable());
 
        DataColumn pidColumn = new DataColumn("PID");
        plans.Tables[0].Columns.Add(pidColumn);
        pidColumn = new DataColumn("Total_Total_Cost");
        pidColumn.DataType = typeof(Decimal);
        plans.Tables[0].Columns.Add(pidColumn);
        pidColumn = new DataColumn("Total_Cost");
        pidColumn.DataType = typeof(Decimal);
        plans.Tables[0].Columns.Add(pidColumn);
        pidColumn = new DataColumn("Plan_Name");
        plans.Tables[0].Columns.Add(pidColumn);
        pidColumn = new DataColumn("SortOrder");
        pidColumn.DataType = typeof(Int64);
        plans.Tables[0].Columns.Add(pidColumn);
 
        DataRow plan = plans.Tables[0].NewRow();
        plan["PID"] = "1";
        plan["Total_Total_Cost"] = 155.41;
        plan["Total_Cost"] = 155.41;
        plan["Plan_Name"] = "Select Value Family";
        plan["SortOrder"] = 0;
        plans.Tables[0].Rows.Add(plan);
 
        DataRow plan2 = plans.Tables[0].NewRow();
        plan2["PID"] = "2";
        plan2["Total_Total_Cost"] = 168.64;
        plan2["Total_Cost"] = 168.64;
        plan2["Plan_Name"] = "Select Med Family";
        plan2["SortOrder"] = 1;
        plans.Tables[0].Rows.Add(plan2);
 
        DataRow plan3 = plans.Tables[0].NewRow();
        plan3["PID"] = "2";
        plan3["Total_Total_Cost"] = 171.62;
        plan3["Total_Cost"] = 171.62;
        plan3["Plan_Name"] = "Select Value Preference";
        plan3["SortOrder"] = 2;
        plans.Tables[0].Rows.Add(plan3);
 
        #endregion Fake populate the dataset
 
        // sort the columns based on the actual sort column that is clicked
        DataTable temp = plans.Tables[0].Copy();
        DataView realSortView = temp.DefaultView;
        realSortView.Sort = PlanGrid.MasterTableView.SortExpressions.GetSortString();
        temp = realSortView.ToTable();
 
        for (Int32 rowIndex = 0; rowIndex < temp.Rows.Count; rowIndex++)
        {
            DataRow row = temp.Rows[rowIndex];
            // apply custom sorting rules that change the values in SortOrder
            // for the purposes of this issue, this logic doesn't matter
            row["SortOrder"] = rowIndex + 1;
        }
 
        // resort based on the modified version of SortOrder
        DataView sortOrderView = temp.DefaultView;
        sortOrderView.Sort = String.Format("{0} ASC""SortOrder");
 
        // this Select() is needed in our real code but not so much in the simplified version
        return sortOrderView.ToTable().Select();
 
    }
 
    /// <summary>
    /// Binds the data to the grid as and when needed, new method available for the Telerik Grid Control
    /// </summary>
    protected void PlanGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        this.PlanGrid.DataSource = loadData();
    }
 
    protected void PlanGrid_OnSortCommand(object sender, GridSortCommandEventArgs e)
    {
        // force a resort of the data
        e.Item.OwnerTableView.DataSource = loadData();
        e.Item.OwnerTableView.Rebind();
    }
 
}



0
Pavlina
Telerik team
answered on 08 Feb 2013, 02:34 PM
Hello Rob,

To prevent this wrong behaviour you should not assign datasource inside OnSortCommand event of the grid. The advantages of using NeedDataSource event is that the developer does not need to write any code handling the logic about when and how the data-binding should be processed.

However, I prepared a sample project based on the provided code which is working properly. Give it a try and let me know if you need additional assistance.

Kind regards,
Pavlina
the Telerik team
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 their blog feed now.
0
Robert
Top achievements
Rank 1
answered on 12 Feb 2013, 11:39 PM
Oops!! That fixed the demo and most of my sorting issues. I think my problem now is because I have my grid in a subcontrol that's drawn conditionally. The first time it is drawn the events don't seem to be attached correctly. I'm going to see if I can move where it's drawn earlier in the process and if the events get attached. Thanks for your help!
Tags
Grid
Asked by
Deyan
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
Deyan
Top achievements
Rank 1
andrea
Top achievements
Rank 1
Waseem
Top achievements
Rank 1
Robert
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or