Sorting the Data in Grid view Manually.


Session["SortTable"] = table; //From Which table  data you are going to bind in gridview  keep that table data in session.

protected void gvtest_Sorting1(object sender, GridViewSortEventArgs e)
    {
        try
        {
            GridView grid = (GridView)sender;
            ViewState["sortexpression"] = e.SortExpression;

            if (ViewState["sortdirection"] == null)
            {
                ViewState["sortdirection"] = "asc";
            }
            else
            {
                if (ViewState["sortdirection"].ToString() == "asc")
                {
                    ViewState["sortdirection"] = "desc";
                }
                else
                {
                    ViewState["sortdirection"] = "asc";
                }
            }
            DataTable dtLive = (DataTable)Session["SortTable "];
            DataView dv = dtLive.DefaultView;
            dv.Sort = ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
            Session["SortTable "] = dv.ToTable("Table");
            grid.DataSource = dv;
            grid.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Comments

Popular posts from this blog

SQL Query :- Create Tables,Primary key,Foreign key,Merge Statment

AngularJS

Check folder and Delete All Files from Existing Folder in C#