GridView.Sorting Event, Doubt?

0

I have the following scenario in which I have a GridView but when I want to sort descending it is not ordering.

below:

    protected void gvdNomeClientes_Sorting(object sender, GridViewSortEventArgs e)
        {
            switch (e.SortExpression)
            {
                case "nome":
                    if (e.SortDirection == SortDirection.Ascending)
                    {
                        gvdNomeClientes.DataSource = lista.OrderBy(x => x.nome).ToList();
                        gvdNomeClientes.DataBind();
                    }
                    else
                    {
                         gvdNomeClientes.DataSource = lista.OrderByDescending(x => x.nome).ToList();
                        gvdNomeClientes.DataBind();
                    }
                    break;
            }
        }

follow the grid:

        <asp:GridView runat="server" 
                ID="gvdClientes" 
                AllowPaging="true" 
                EmptyDataText="Nenhum registro foi inserido..."
                AutoGenerateColumns="false" 
                OnRowCommand="gvdClientes_RowCommand"
                AllowSorting="true"
                OnSorting="gvdClientes_Sorting">

                <Columns>
                    <asp:BoundField SortExpression="nome" DataField="_nomeCliente" HeaderText="Nome" HtmlEncode="False" FooterText="">
                        <ItemStyle BorderStyle="Solid" BorderWidth="2px" HorizontalAlign="Center" VerticalAlign="Middle"/>
                    </asp:BoundField>
                    <asp:BoundField SortExpression="cidade" DataField="_cidade" HeaderText="Cidade" HtmlEncode="False" FooterText="">
                        <ItemStyle BorderStyle="Solid" BorderWidth="2px" HorizontalAlign="Center" VerticalAlign="Middle"/>
                    </asp:BoundField>
                </Columns>

            </asp:GridView>
    
asked by anonymous 21.01.2015 / 13:30

2 answers

0

Well responding to myself rs .. I came to a solution to this problem, I hope it helps other people who come across the same situation:

 protected void gvdNomeClientes_Sorting(object sender, GridViewSortEventArgs e)
 {
        if (ViewState["sortColumn"].ToString() == e.SortExpression.ToString())
            {
                if ("ASC" == ViewState["sortDirection"].ToString())
                {
                    ViewState["sortDirection"] = "DESC";
                    carregaNomeClientes(ListarClientes().OrderByDescending(x => x.nome).ToList());
                }
                else
                {
                    ViewState["sortDirection"] = "ASC";
                    carregaNomeClientes(ListarClientes().OrderBy(x => x.nome).ToList());
                }
            }
            else
            {
                ViewState["sortColumn"] = e.SortExpression.ToString();
                ViewState["sortDirection"] = "ASC";
                carregaNomeClientes(ListarClientes().OrderBy(x => x.nome).ToList());
            }

 }

I initially also declared in the Page_Load of the page:

            ViewState["sortColumn"] = String.Empty;
            ViewState["sortDirection"] = String.Empty;
    
22.01.2015 / 18:02
1

I noticed that when it is to sort descending, you do return the list to another gridview, I believe the correct one is to add the list in the gridview gvdCustomers

The code below should work.

 protected void gvdNomeClientes_Sorting(object sender, GridViewSortEventArgs e)
        {
            switch (e.SortExpression)
            {
                case "nome":
                    if (e.SortDirection == SortDirection.Ascending)
                    {
                        gvdNomeClientes.DataSource = lista.OrderBy(x => x.nome).ToList();
                        gvdNomeClientes.DataBind();
                    }
                    else
                    {
                        gvdNomeClientes.DataSource = lista.OrderByDescending(x => x.nome).ToList();
                        gvdNomeClientes.DataBind();
                    }
                    break;
            }
        }
    
21.01.2015 / 13:49