Pregunta

¿Qué debo hacer para realizar la ordenación en la vista de cuadrícula?

Por favor, ayuda

¿Fue útil?

Solución

Se puede aplicar un código como el siguiente:

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
     DataTable dataTable = GridView1.DataSource as DataTable;

     if (dataTable != null)
     {
          DataView dataView = new DataView(dataTable);
          dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

          GridView1.DataSource = dataView;
          GridView1.DataBind();
     }
}

private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
     string newSortDirection = String.Empty;

     switch (sortDirection)
     {
          case SortDirection.Ascending:
              newSortDirection = "ASC";
          break;

          case SortDirection.Descending:
              newSortDirection = "DESC";
          break;
     }

     return newSortDirection;
}

Con este código, su definición GridView debe decir:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowSorting="true" OnSorting="GridView1_Sorting">
<Columns>
    <asp:BoundField DataField="Name" HeaderText="People Names" SortExpression="Name" />
    <asp:BoundField DataField="Age" HeaderText="People Ages" SortExpression="Age" />
</Columns>
</asp:GridView>

Otros consejos

No estoy seguro si ya ha agregado el evento o no en su código detrás.

Se han fijado AllowSorting="true" para el GridView y por lo tanto lo que necesita tienen controlador de eventos para su evento de clasificación.

< asp:GridView AllowSorting=true ID="GridView1" runat="server" 
  OnSorting="GridView1_Sorting" >
    ...
< /asp:GridView >


protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)

{

     //Add your code here for handling

}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top