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
Post a Comment