My Telerik RadGrid will not sort no matter what code I put in it. I have tried many things, and everytime I click on the headers, the arrow and color will change, but the data will stay in the same order. This is my latest chunk of code. (I got this from the Telerik website) Please tell me what I am doing wrong here.
<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">
<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>
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();
}
}
Instead of doing this:
tableView.SortExpressions.AddSortExpression(expression);
tableView.Rebind();
Do this:
radProductsGrid.MasterTableView.SortExpressions.AddSortExpression(expression);
radProductsGrid.Rebind();