Как включить сортировку столбцов в элементе управления ASP.NET Gridview - PullRequest
0 голосов
/ 02 апреля 2012

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 голосов
/ 02 апреля 2012

Пожалуйста, обработайте событие 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;
    }
0 голосов
/ 02 апреля 2012

Есть ли у вас столбец данных, который называется Ministry Name. Или это называется MinistryName. Потому что если столбец называется MinistryName. Тогда выражение сортировки должно быть:

SortExpression="MinistryName"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...