Frage

Was soll ich tun, die Sortierung in der Rasterdarstellung durchführen?

Bitte Hilfe

War es hilfreich?

Lösung

Sie können Code wie folgt implementieren:

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

Mit diesem Code, sollten Sie Ihre Gridview Definition lesen:

<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>

Andere Tipps

Nicht sicher, ob Sie bereits das Ereignis oder die nicht in Ihrem Code hinter hinzugefügt haben.

Sie haben AllowSorting="true" Satz für das Gridview und deshalb müssen Sie hat Event-Handler für sein Sorting-Ereignis aus.

< 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

}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top