如何在网格视图中对列进行排序

时间:2012-06-22 11:31:16

标签: asp.net

我试过这个

<asp:GridView ID="GvDetails" **runat="server" Width="100%" AutoGenerateColumns="False"
    AllowPaging="True" CssClass="clGrid" PageSize="25" OnRowEditing="GvDetails_RowEditing"
     OnRowCommand="GvDetails_RowCommand"  OnPageIndexChanging="GvDetails_PageIndexChanging"
     EnableModelValidation="True"  AllowSorting="true" EnableSortingAndPagingCallbacks="true"
    onselectedindexchanged="GvDetails_SelectedIndexChanged">
    <Columns>

     <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" >
        <HeaderStyle Width="35%" CssClass="gridHeader"></HeaderStyle>
                                <ItemStyle CssClass="gridItem" HorizontalAlign="Left"></ItemStyle>
      </asp:BoundField>
     </Columns>
</asp:GridView>

但不工作:(

1 个答案:

答案 0 :(得分:0)

如果您希望对DataSource进行排序,则需要以编程方式对GridView进行排序。

来自Telerik和DevExpress的控件可以为您执行此操作,但您必须为它们付费。

幸运的是,排序DataSource非常简单。

<asp:Button ID="btSort" runat="server">Sort Alphabetically</asp:Button>

然后点击甚至调用此排序功能

    public void SortAlphabetically()
    {
        List<string> names = // However you get your names

        // Sort from A-Z using Linq
        var sort = from s in names
                   orderby s
                   select s;

        // Set the DataSource and Bind the data
        GvDetails.DataSource = sort;
        GvDetails.DataBind();
    }