如何在ASP.Net中对GridView记录进行排序

时间:2015-11-28 06:22:08

标签: asp.net sorting gridview

我想对databound GridView进行排序。我在GridView中的子网络表单中添加了UpdatePanel。以下是我的代码。

   <form id="CategoryForm" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="CategoryID" 
                OnRowDataBound="OnRowDataBound" OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit" 
                OnRowUpdating="OnRowUpdating" OnRowDeleting="OnRowDeleting" OnPageIndexChanging="OnPageIndexChanging" 
                EmptyDataText="No records has been added." ShowFooter="true" CssClass="table table-hover table-striped"
                AllowPaging="true" PageSize="6" AllowSorting="true" >
                <PagerSettings Mode="NumericFirstLast" PageButtonCount="4" Position="Top" />
                <Columns>
                    <asp:TemplateField HeaderText="Name" ItemStyle-Width="150">
                        <HeaderTemplate>
                            <asp:Label ID="filterName" Text="Name" runat="server"></asp:Label>
                            </br>
                            <asp:Button ID="btnNameSort" OnClick="btnSortName_Click" runat="server"></asp:Button>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblCatName" runat="server" Text='<%# Eval("CategoryName") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtCatName" runat="server" Text='<%# Eval("CategoryName") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtCatNameAdd" runat="server" ValidationGroup="validation"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="rfvName" runat="server" ControlToValidate="txtCatNameAdd" ValidationGroup="validation" ErrorMessage="Cannot Be Empty." />
                        </FooterTemplate>
                    </asp:TemplateField>
<asp:TemplateField ItemStyle-Width="150">
                        <FooterTemplate>
                            <asp:Button class="btn btn-primary" ID="btnAdd" Text="Add" runat="server" ValidationGroup="validation" OnClick="Insert"></asp:Button>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="150" />
                </Columns>
            </asp:GridView>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="GridView1" />
        </Triggers>
</asp:UpdatePanel>
</form>

<asp:GirdView>标记中声明的所有事件都是CRUD的常规事件。在代码中,我跳过DetailsDate等的其他列。现在我想对GridView中的任何列进行排序。怎么做?感谢...

1 个答案:

答案 0 :(得分:0)

<asp:GridView ID="GridView1" runat="server" AllowSorting="true" 
      OnSorting="GridView1_Sorting">
</asp:GridView> 


protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{ 
    SetSortDirection(SortDireaction);
    if (dataTable != null)
    {
        //Sort the data.
        dataTable.DefaultView.Sort = e.SortExpression + " " +_sortDirection;
        GridView1.DataSource = dataTable;
        GridView1.DataBind();
        SortDireaction = _sortDirection;
    }
} 
protected void SetSortDirection(string sortDirection)
{
    if (sortDirection == "ASC")
    {
        _sortDirection = "DESC";
    }
    else
    {
        _sortDirection = "ASC";
    }
}