如何在ASP.NET Gridview控件中启用排序列

时间:2012-04-02 06:49:35

标签: asp.net gridview

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无效,此

上的任何输入

2 个答案:

答案 0 :(得分:0)

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

SortExpression="MinistryName"

答案 1 :(得分:0)

请处理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;
    }