使用绑定字段进行Gridview排序

时间:2012-05-06 18:21:10

标签: c# asp.net .net

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        AllowPaging="true" AllowSorting="true" PageSize="10" DataKeyNames="CategoryID" 
        onselectedindexchanged="GridView1_SelectedIndexChanged" 
        GridLines="Vertical" onrowediting="GridView1_RowEditing" 
        onrowcancelingedit="GridView1_RowCancelingEdit" 
        onrowupdating="GridView1_RowUpdating" onsorted="GridView1_Sorted" 
        onsorting="GridView1_Sorting">
        <Columns>
        <asp:BoundField DataField="categoryid" HeaderText="ID" SortExpression="CategoryID" />        
        </Columns>        
    </asp:GridView>

排序不起作用。我错过了什么?

1 个答案:

答案 0 :(得分:1)

在排序事件处理程序中,您可以更改Data表上的sort属性。

  dt.DefaultView.Sort = e.SortExpression + " ASC";
  gv.DataSource = dt;
  gv.DataBind();

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting.aspx