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>