【问题标题】:how to enable sorting columns in ASP.NET Gridview control如何在 ASP.NET Gridview 控件中启用排序列
【发布时间】:2012-04-02 06:49:35
【问题描述】:

ASPX 代码。

<asp:GridView ID="gidtest" runat="server" AutoGenerateColumns="False" BackColor="White"
        BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="100%"
        AllowPaging="True" **AllowSorting="True"** 
        OnPageIndexChanging="gidtest_PageIndexChanging">
        <Columns>
            <asp:BoundField DataField="MinistryName" HeaderText="Name" SortExpression="Ministry Name"
                ItemStyle-Width="40%" >
<ItemStyle Width="40%"></ItemStyle>
            </asp:BoundField>
        </Columns>
        <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
        <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
        <PagerSettings FirstPageText="first" LastPageText="last" Mode="NumericFirstLast" PageButtonCount="2"/>
        <RowStyle BackColor="White" ForeColor="#330099" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
        <SortedAscendingCellStyle BackColor="#FEFCEB" />
        <SortedAscendingHeaderStyle BackColor="#AF0101" />
        <SortedDescendingCellStyle BackColor="#F6F0C0" />
        <SortedDescendingHeaderStyle BackColor="#7E0000" />
    </asp:GridView>

SORTEXpression 不起作用,对此有任何输入

【问题讨论】:

标签: asp.net gridview


【解决方案1】:

您的数据源中有一个名为Ministry Name 的列。还是叫MinistryName。因为如果该列被称为MinistryName。那么排序表达式应该是:

SortExpression="MinistryName"

【讨论】:

    【解决方案2】:

    请处理 OnSorting 事件

    <asp:GridView ID="gidtest" runat="server" AutoGenerateColumns="False" BackColor="White"
            BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="100%"
            AllowPaging="True" **AllowSorting="True"** 
            OnPageIndexChanging="gidtest_PageIndexChanging" OnSorting="gidtest_Sorting"
    >
            <Columns>
                <asp:BoundField DataField="MinistryName" HeaderText="Name" SortExpression="Ministry Name"
                    ItemStyle-Width="40%" >
    <ItemStyle Width="40%"></ItemStyle>
                </asp:BoundField>
            </Columns>
            <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
            <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
            <PagerSettings FirstPageText="first" LastPageText="last" Mode="NumericFirstLast" PageButtonCount="2"/>
            <RowStyle BackColor="White" ForeColor="#330099" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
            <SortedAscendingCellStyle BackColor="#FEFCEB" />
            <SortedAscendingHeaderStyle BackColor="#AF0101" />
            <SortedDescendingCellStyle BackColor="#F6F0C0" />
            <SortedDescendingHeaderStyle BackColor="#7E0000" />
        </asp:GridView>
    
    
    
    
    
    
    protected void gidtest_Sorting(object sender, GridViewSortEventArgs e)
        {
    
            try
            {
                //Retrieve the table from the session object.
                DataTable dt = GetData();
    
                if (dt != null)
                {
    
                    //Sort the data.
                    dt.DefaultView.Sort = e.SortExpression + " " + **GetSortDirection**(e.SortExpression);
                    gidtest.DataSource = dt;
                    gidtest.DataBind();
                }
            }
            catch (Exception ex)
            {
    
                throw ex;
            }
    
        }
    
    
    
    
     private string GetSortDirection(string column)
        {
    
            // By default, set the sort direction to ascending.
            string sortDirection = "ASC";
    
            // Retrieve the last column that was sorted.
            string sortExpression = ViewState["SortExpression"] as string;
    
            if (sortExpression != null)
            {
                // Check if the same column is being sorted.
                // Otherwise, the default value can be returned.
                if (sortExpression == column)
                {
                    string lastDirection = ViewState["SortDirection"] as string;
                    if ((lastDirection != null) && (lastDirection == "ASC"))
                    {
                        sortDirection = "DESC";
                    }
                }
            }
    
            // Save new values in ViewState.
            ViewState["SortDirection"] = sortDirection;
            ViewState["SortExpression"] = column;
    
            return sortDirection;
        }
    

    【讨论】:

    • Sunil,Sort 表达式怎么样?我们应该给出的是 DataFieldName 或 HeaderName
    • Sunil 对于下面的行我们应该使用 Session 吗? //从会话对象中检索表。数据表 dt = GetData();
    • 取决于您的数据。如果您的数据很大,那么服务器性能将会受到影响。尝试再次点击 db 并避免使用会话来存储数据表。
    • 在您的案例中,您将其发布为答案。您必须为您的 Gridview 设置“Enableviewstate=True”。
    猜你喜欢
    • 1970-01-01
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    相关资源
    最近更新 更多