Telerik RadGrid排序ASP.NET C#

时间:2013-10-30 14:06:05

标签: c# asp.net sorting telerik telerik-grid

无论我输入什么代码,我的Telerik RadGrid都不会排序。我尝试了很多东西,每次点击标题时,箭头和颜色都会改变,但数据将保持相同的顺序。这是我最新的一大堆代码。 (我是从Telerik网站上得到的)请告诉我这里我做错了什么。

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

3 个答案:

答案 0 :(得分:0)

而不是这样做:

tableView.SortExpressions.AddSortExpression(expression);
tableView.Rebind();

这样做:

radProductsGrid.MasterTableView.SortExpressions.AddSortExpression(expression);
radProductsGrid.Rebind();

答案 1 :(得分:0)

检查javascript onclick()事件,它是Drops Sort事件

            var hasChanges, inputs, dropdowns, editedRow;

            function RowClick(sender, eventArgs) {
                if (editedRow && hasChanges) {
                    hasChanges = false;
                    if (confirm("Update changes?")) {

                        $find("<%= RadGrid1.ClientID %>").get_masterTableView().updateItem(editedRow);
                    }
                }
            }

答案 2 :(得分:-1)

FROM