Sorting Gridview event, do you doubt?

Asked

Viewed 193 times

0

I have the following scenario in which I have a Gridview but when I want to order disbelief it is not ordering.

goes 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;
            }
        }

follows 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>
  • 1

    Don’t you think that "doesn’t work" is a little too generic to describe something that helps people help you? Put more details than this means, what happens, what your expectation, what you have tried alternatively.

  • thanks for the signage, I wrote fast rs, what happens is the following he orders crescently, but the decreasing t t working. I didn’t try to do it any other way...

2 answers

1

I noticed that when it is to sort down, you do return the list to another gridview, I think the correct is to add the list in gridview gvdNomeClients

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;
            }
        }
  • it still didn’t work. :(

  • What do you mean "it didn’t work"?

  • he is not ordering decreasingly, increasingly he works..

  • 1

    Well, based on what’s in the code, the only way not to order down is if he doesn’t get into "Isis". could put a break point and tell me what happens?

  • It never arrives at Else, every time I click it always goes on (Sortdirection.Ascending)

  • Right, where do you click p/ call this event?

  • i click on the name of the column that looks like this: on the <Asp:Templatefield Sortexpression="name">

  • Could I have an image please?

  • Paul edited the question and put the code of the grid, in case I click in the column "Name"

  • I solved Paul, note above the solution, what was happening is that he never reached Isis, that is he could not say what would be the state of Sorting if it was growing or decreasing.

Show 5 more comments

0


Good responding to myself rs.. I have reached 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());
            }

 }

initially I also declared in the page Page_load:

            ViewState["sortColumn"] = String.Empty;
            ViewState["sortDirection"] = String.Empty;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.