Friday, 19 October 2012

gridveiw sorting event in asp.net c#

  protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string newSortDirection;
        if (ViewState["SortDirection"] != null)
            newSortDirection = ViewState["SortDirection"].ToString();
        else if (e.SortDirection   == SortDirection.Ascending)
            newSortDirection = "ASC";
        else
           newSortDirection = "DESC";
       
        
        DataSet ds1 = (DataSet)ViewState["ds"];
        if (ds1.Tables.Count > 0 && ds1.Tables[0].Rows.Count > 0)
        {
            DataTable tb = ds1.Tables[0];
            if (tb != null)
            {
                DataView dv = new DataView(tb);
                dv.Sort = e.SortExpression + " " + newSortDirection;
                GridView1.DataSource = dv;
                GridView1.DataBind();
                if (ViewState["SortDirection"] != null)
                {
                    if (ViewState["SortDirection"].ToString() == "ASC")
                        ViewState["SortDirection"] = "DESC";
                    else
                        ViewState["SortDirection"] = "ASC";
                }
                else if (e.SortDirection == SortDirection.Ascending)
                    ViewState["SortDirection"] = "DESC";
                else
                    ViewState["SortDirection"] = "ASC";
            }
        }
 
    }

No comments:

Post a Comment