protected void GridViewUserPolicies_Sorting(object sender, GridViewSortEventArgs e) { DataTable datatable = (DataTable)ViewState["table"]; if (datatable != null) { DataView view = new DataView(datatable); if (Convert.ToString(ViewState["sort"]).Contains("ASC") || Convert.ToString(ViewState["sort"]) == "") { view.Sort = e.SortExpression + " " + "DESC"; ViewState["sort"] = view.Sort.ToString(); } else { view.Sort = e.SortExpression + " " + "ASC"; ViewState["sort"] = view.Sort.ToString(); } GridViewUserPolicies.DataSource = view; GridViewUserPolicies.DataBind(); } }
public void bindGrid() { try { SqlCommand listPolicies = new SqlCommand("spListOfPolicies", connection); listPolicies.CommandType = CommandType.StoredProcedure; connection.Open(); SqlDataAdapter sda = new SqlDataAdapter(listPolicies); DataTable dt = new DataTable(); sda.Fill(dt); ViewState["VSTable"] = dt; GridViewUserPolicies.DataSource = dt; GridViewUserPolicies.DataBind(); } catch (Exception e) { throw e; } finally { connection.Close(); } }