Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I keep sorting order while paging in gridView?
    primarykey
    data
    text
    <p>I am displaying information using gridView. It works absolutely perfect for me except for one detail. If the user sorts a column and then clicks to go to another page then the sorting is lost. Basically I need the sorting to persist while paging occurs, any help would be most appreciated. Here is the code I have:</p> <pre><code>public partial class DynamicGridView : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { if (_selectCmd != string.Empty) BindData(); } private void BindData() { DataTable dt; if (ViewState[gridView.ClientID + "datatable"] == null || (string)ViewState[gridView.ClientID + "selectCommand"] != _selectCmd) { ViewState[gridView.ClientID + "selectCommand"] = _selectCmd; dt = Classes.SQL.GetDataTable(_selectCmd); if (dt != null) { ViewState[gridView.ClientID + "datatable"] = dt; if (dt.Columns.Count != gridView.Columns.Count) { gridView.Columns.Clear(); foreach (DataColumn column in dt.Columns) AddBoundField(column); } } } else dt = (DataTable)ViewState[gridView.ClientID + "datatable"]; gridView.DataSource = dt; gridView.DataBind(); } private void AddBoundField(DataColumn column) { BoundField field = new BoundField(); HandleCustomDataFormatting(ref field, column); field.DataField = column.ColumnName; field.HeaderText = column.ColumnName; field.ReadOnly = true; field.SortExpression = column.ColumnName; field.ControlStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#333333"); field.ItemStyle.Width = System.Web.UI.WebControls.Unit.Pixel(70); gridView.Columns.Add(field); } private void HandleCustomDataFormatting(ref BoundField field, DataColumn column) { if (column.DataType == typeof(System.DateTime)) switch (column.ColumnName) { case "InvalidDate": case "NewCustomerValidated": case "ValidatedDate": field.DataFormatString = "{0:d}";//exclude time info break; default: field.DataFormatString = "{0:g}"; break; } } private string GetSortDirection(string sortColumn) { string sortDirection = "ASC"; string lastSortColumn = (string)ViewState[gridView.ClientID + "SortExpression"]; if (lastSortColumn != null &amp;&amp; lastSortColumn == sortColumn) { string lastDirection = (string)ViewState[gridView.ClientID + "SortDirection"]; if (lastDirection != null &amp;&amp; lastDirection == "ASC") sortDirection = "DESC"; } ViewState[gridView.ClientID + "SortExpression"] = sortColumn; ViewState[gridView.ClientID + "SortDirection"] = sortDirection; return sortDirection; } private string _selectCmd = string.Empty; public string SelectCommand { set { _selectCmd = value; } } protected void GridView_Sorting(object sender, GridViewSortEventArgs e) { DataTable dt = (DataTable)ViewState[gridView.ClientID + "datatable"]; dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression); BindData(); } protected void GridView_PageChange(object sender, GridViewPageEventArgs e) { gridView.PageIndex = e.NewPageIndex; BindData(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload