.Net GridView - GridView Sorting Example, How to Sort GridView Data, GridView control in ASP.Net 2.0/3.5/4.0

.Net GridView GridView.Sorting Event

public event GridViewSortEventHandler Sorting

<asp:GridView OnSorting="GrExampleIDViewSortEventHandler" />

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  protected voExampleID ExampleGridView_Sorting(object sender, GrExampleIDViewSortEventArgs e)
  {

    //Retrieve the table from the session object.
    DataTable dt = Session["ExampleTable"] as DataTable;

    if (dt != null)
    {

      //Sort the data.
      dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
      ExampleGridView.DataSource = Session["ExampleTable"];
      ExampleGridView.DataBind();
    }

  }

  private string GetSortDirection(string column)
  {

    // By default, set the sort direction to ascending.
    string sortDirection = "ASC";

    // Retrieve the last column that was sorted.
    string sortExpression = ViewState["SortExpression"] as string;

    if (sortExpression != null)
    {
      // Check if the same column is being sorted.
      // Otherwise, the default value can be returned.
      if (sortExpression == column)
      {
        string lastDirection = ViewState["SortDirection"] as string;
        if ((lastDirection != null) && (lastDirection == "ASC"))
        {
          sortDirection = "DESC";
        }
      }
    }

    // Save new values in ViewState.
    ViewState["SortDirection"] = sortDirection;
    ViewState["SortExpression"] = column;

    return sortDirection;
  }

  protected voExampleID Page_Load(object sender, EventArgs e)
  {

    if (!Page.IsPostBack)
    {

      // Create a new table.
      DataTable ExampleTable = new DataTable("TaskList");

      // Create the columns.
      ExampleTable.Columns.Add("ExampleID", typeof(int));
      ExampleTable.Columns.Add("ExampleDescription", typeof(string));

      //Add data to the new table.
      for (int i = 0; i < 10; i++)
      {
        DataRow tableRow = ExampleTable.NewRow();
        tableRow["ExampleID"] = i;
        tableRow["ExampleDescription"] = "Task " + (10 - i).ToString();
        ExampleTable.Rows.Add(tableRow);
      }

      //Persist the table in the Session object.
      Session["ExampleTable"] = ExampleTable;

      //Bind the GrExampleIDView control to the data source.
      ExampleGridView.DataSource = Session["ExampleTable"];
      ExampleGridView.DataBind();

    }

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Sorting Example asp:GridView </title>
</head>
<body>
    <form ExampleID="form1" runat="server">
    <div>

      <asp:GridView ExampleID="ExampleGridView" runat="server"
        AllowSorting="true"
        OnSorting="ExampleGridView_Sorting" >
      </asp:GridView>

    </div>
    </form>
</body>
</html>

The Sorting event is raised when the hyperlink to sort a column is clicked, but before the GridView control handles the sort operation. This enables you to provide an event-handling method that performs a custom routine, such as canceling the sorting operation, whenever this event occurs.

A GridViewSortEventArgs object is passed to the event-handling method, which enables you to determine the sort expression for the column and to indicate that the selection operation should be canceled. To cancel the selection operation, set the Cancel property of the GridViewSortEventArgs object to true.

For information about how to programmatically initiate a sort operation.


<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ASP.Net GridView Sorting Example- How to Sort GridView Data</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Green">Sorting In GridView</h2>
        <asp:SqlDataSource
             ID="SqlDataSourceExample"
             runat="server"
             ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
             SelectCommand="SELECT TOP 6 ItemID, ItemName, ItemUnitPrice FROM TableProducts"
             >
        </asp:SqlDataSource>
        <asp:GridView
             ID="GridViewExample"
             runat="server"
             DataSourceID="SqlDataSourceExample"
             AutoGenerateColumns="false"
             BackColor="Red"
             ForeColor="Goldenrod"
             BorderColor="Yellow"
             BorderStyle="Groove"
             AllowSorting="true"
             >
            <Columns>
                <asp:BoundField SortExpression="ItemID" HeaderText="Product ID" DataField="ItemID" />
                <asp:BoundField SortExpression="ItemName" HeaderText="Product Name" DataField="ItemName" />
                <asp:BoundField SortExpression="ItemUnitPrice" HeaderText="Unit Price" DataField="ItemUnitPrice" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>
Tags: , , , , ,
Hot on Web:


About author