protected void SortRecords(object sender, GridViewSortEventArgs e) { DataTable dataTable = GridDataSource(); if (dataTable != null) { DataView dataView = new DataView(dataTable); dataView.Sort = GetSortExpression(e); grdBlock.DataSource = dataView; grdBlock.DataBind(); } } private string GetSortExpression(GridViewSortEventArgs e) { string sortDirection = string.Empty; // if clicked on the same column twice then let it toggle the sort order, else reset to ascending if (ViewState["SortExpression"] != null) { if (!ViewState["SortExpression"].ToString().Equals(e.SortExpression.ToLower())) { ViewState["SortDirection"] = null; } } if (ViewState["SortDirection"] != null) { if (ViewState["SortDirection"].ToString().Equals("ASC")) { sortDirection = "DESC"; ViewState["SortDirection"] = "DESC"; } else { sortDirection = "ASC"; ViewState["SortDirection"] = "ASC"; } } else { ViewState["SortDirection"] = "ASC"; } ViewState["SortExpression"] = e.SortExpression.ToLower(); return e.SortExpression + " " + sortDirection; } protected DataTable GridDataSource() { DataTable dTable = new DataTable(); DataAccessClass dao = new DataAccessClass(); DataSet ds = new DataSet(); try { SqlParameter Blockname = new SqlParameter("@Blockname", null); SqlParameter[] DBblock = new SqlParameter[1] { Blockname }; ds = dao.ExecuteSelect("spGetBlockData", DBblock); dTable = ds.Tables[0]; } catch (Exception ex) { } finally { ds.Dispose(); } return dTable; }
Thanks, Sanjay
Login to post response