【问题标题】:How to sort GridView Records in ASP.Net如何在 ASP.Net 中对 GridView 记录进行排序
【发布时间】:2016-03-02 07:18:35
【问题描述】:

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

   <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>

&lt;asp:GirdView&gt; 标签中声明的所有事件都是 CRUD 的通用事件。在代码中,我跳过了DetailsDate 等的其他列。现在我想对 GridView 中的任何列进行排序。怎么做?谢谢...

【问题讨论】:

    标签: asp.net sorting gridview


    【解决方案1】:
    <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";
        }
    } 
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 1970-01-01
    • 2010-09-17
    • 2012-11-02
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    相关资源
    最近更新 更多