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

RadGrid Custom Sorting with header context menu

5 Answers 361 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dee
Top achievements
Rank 1
Dee asked on 23 Nov 2011, 05:55 PM
Hi,

I'm using a radgrid with custom paging & custom sorting. Custom sorting by clicking on headers works fine except that when I right click on the header and use the context menu to sort columns, it sorts them with natural sort and does not invoke my OnSortCommand server side callback. 

<telerik:RadGrid ID="SearchResultGrid"
        AllowPaging="True" AllowCustomPaging="true" EnableViewState="True" AllowFilteringByColumn="False"
        AllowSorting="true" AllowNaturalSort="false" AllowCustomSorting="true" OnSortCommand="SearchResultGrid_OnSort"
        AutoGenerateColumns="False" runat="server" PageSize="20"
        EnableLinqExpressions="false" EnableAJAX="false" ItemStyle-Wrap="false" AlternatingItemStyle-Wrap="false" ...>
    <HeaderContextMenu OnItemClick="HeaderContextMenu_ItemClick" OnItemCreated="HeaderContextMenu_ItemCreated"
            OnPreRender="HeaderContextMenu_PreRender">
        <CollapseAnimation Duration="200" Type="OutQuint" />
    </HeaderContextMenu>
    ...

How can I make the header context menu sorting use custom sorting?

5 Answers, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 25 Nov 2011, 01:45 PM
Hello Dee,

The SortCommand is not firing when sorting with header context menu. You can wire an event handler for the ItemClick event of the header context menu and there put the logic for custom sorting:
protected void HeaderContextMenu_ItemClick(object sender, RadMenuEventArgs e)
        {
            if (e.Item.Text=="Sort Ascending")
            {
                //custom code here
            }
        }

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
Dee
Top achievements
Rank 1
answered on 25 Nov 2011, 04:53 PM
Pavlina,

Thanks for the reply, I will try that.

How can I find out the column name on which the Sort Ascending header item was clicked? 
protected void HeaderContextMenu_ItemClick(object sender, RadMenuEventArgs e)
        {
            if (e.Item.Text=="Sort Ascending")
            {
                // How can I find out the column name here?
            }
        }

Also,  already have  AllowNaturalSort="false" but the header context menu still does a client side sort when I click Sort Ascending item on the header. How can I prevent that?

Thanks,
Dee
0
Pavlina
Telerik team
answered on 25 Nov 2011, 05:24 PM
Hello Dee,

You can use the code snippet below to find the column on which the Sort Ascending was clicked:
if (e.Item.Text=="Sort Ascending")
{              
    string columnName = e.Item.Attributes["columnUniqueName"];
    //get instance to the column from the respective GridTableView
    GridColumn gridColumn = RadGrid1.MasterTableView.GetColumnSafe(columnName);
}

Give it a try and let me know how it goes.

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
Pavlina
Telerik team
answered on 25 Nov 2011, 05:37 PM
Hello Dee,

You can use the code snippet below to find the column on which the Sort Ascending was clicked:
if (e.Item.Text=="Sort Ascending")
{              
    string columnName = e.Item.Attributes["columnUniqueName"];
    //get instance to the column from the respective GridTableView
    GridColumn gridColumn = RadGrid1.MasterTableView.GetColumnSafe(columnName);
}

Give it a try and let me know how it goes.

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
Rob
Top achievements
Rank 1
answered on 16 Apr 2013, 03:11 PM
I am trying to make this work on a grid with multiple GridImageColumns and a GridTemplateColumn.

The following line always resolves into an empty string for all columns:
 string columnName = e.Item.Attributes["columnUniqueName"];  

Using
string columnName = e.Item.Attributes["ColumnName"];
will resolve a name for the simpler GridBoundColumns, but not my image or template columns.

Is there something I'm missing or has the most recent answer on this thread become dated?
Tags
Grid
Asked by
Dee
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
Dee
Top achievements
Rank 1
Rob
Top achievements
Rank 1
Share this question
or