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();
}
}