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

Sort by column that is not in the grid

2 Answers 75 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Baal
Top achievements
Rank 1
Baal asked on 11 Feb 2016, 07:12 PM

Hi, I have a RadGrid in my page and one of the columns is Size. Size is a combination of dimensions. Let's say, for the sake of simplicity, we have three dimensions - length, width, and height..When displaying, it will not always be in the format of [length x width] or [length x width x height], some might need to be displayed as [width x height] only or ]width x length x height]. We already have appropriate sql function that formats the display appropriately.

Another thing is this field is of string type. So when sorting, we will have issues.In terms like 12 can come up before 9. So what we have done is create another sql function that helps sort it appropriately. We have a varchar(30) columns where we plug in 10 length value for each dimension - 1st five is for the whole number part, 2nd five for decimal part, multiplied by three dimensions so 30 characters length always. Eg.

1.Length    Width    Height    Display           SortValue
2.10        12.125   7         10 x 12.125 x 7   000100000000012125000000700000
3.5         7.75     9.5       7.75 x 5 x 9.5    000077500000005000000000950000
4.8.5       9        2.0125    8.5 x 9 x 2.0125  000085000000009000000000201250
5.2.5       8                  2.5 x 8           000025000000008000000000000000

I am getting data using NeedDataSource. My sql query gets only as much data as is needed to show in the page. So if grid page size is 10, I get first 10 rows only for page 1. When user navigates to second page, code calls NeedDataSource again to get the next 20 rows of data.

In SortCommand method, all I do is populate a SortDictionary with appropriate field name and sort order and rebind grid again.

private Dictionary<String, String> SortByDictionary
{
    get { return ViewState["sortBy"] == null ? new Dictionary<string, string>() : (Dictionary<String, String>)ViewState["sortBy"]; }
    set { ViewState["sortBy"] = value; }
}
 
protected void rgResult_SortCommand(object sender, GridSortCommandEventArgs e)
{
    if (e.CommandArgument != null)
    {
        var key = e.CommandArgument.ToString();
 
        if (SortByDictionary == null)
            SortByDictionary = new Dictionary<string, string>();
 
        if (!SortByDictionary.ContainsKey(key))
            SortByDictionary.Add(key, String.Empty);
 
        if (e.NewSortOrder == GridSortOrder.None)
            SortByDictionary.Remove(key);
        else
            SortByDictionary[key] = e.NewSortOrder == GridSortOrder.Ascending ? "ASC" : "DESC";
    }
 
    e.Item.OwnerTableView.DataSource = GetData(false);
    e.Item.OwnerTableView.Rebind();
}

 

In the GetData function, to create the order by part of the sql query, I loop through all the keys in SortDictionary and change order column to SortValue if I see Display there. Sql query comes up fine and executing it in SSMS presents me the results correctly. For example, from the data example above, it gives me line 5 first, then 3, 4 and 2 as expected.

However, problem lies when displaying in grid. Because user had clicked sort by Display column, grid resorts the data again. So the data gets displayed as line 2, 5, 3, and 4.

My question is - is there a way to prevent grid from resorting? I am using Telerik.Web.UI version 2013.3.1324.40.

  

Thank you.

2 Answers, 1 is accepted

Sort by
0
Accepted
Viktor Tachev
Telerik team
answered on 16 Feb 2016, 12:30 PM
Hello Baal,

Based on the description of the scenario you would need to use custom sorting for RadGrid. Thus, you can ensure that the items are ordered based on your requirements.

In order to prevent the RadGrid from performing sort operations you need to set the AllowCustomSorting property of the MasterTableView to true. Then you would need to implement the custom sorting logic in the SortCommand event handler.

Check out the following resources that describe the functionality in more detail.




Regards,
Viktor Tachev
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
0
Baal
Top achievements
Rank 1
answered on 23 Feb 2016, 05:51 PM

Thank you. All I needed was AllowCustomSorting = true in my MasterTable.

I was already using custom sorting, which is why I was able to sort on a per page basis using custom fields. It was just that the page itself was also sorting. It is working fine now. 

Tags
Grid
Asked by
Baal
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Baal
Top achievements
Rank 1
Share this question
or