GridView sorting and paging using custom data method
Hi
When you are using a custom method or data object that returns a Dataset or DataTable and binding that to data control such as GridView or DetailsView, sorting and paging is little tricky than usual.
using stored procedure with custom method to bind data to GridView is explained in Dynamic Search using SQL Stored Procedure
Next requirement is to GridView Sorting and Paging. This article explains how to implment GridView paging and sorting using custom method.
/// <summary> /// Handle Gridview paging event /// and bind Search results data to GridView /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_PageIndexChanging (object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; //Bind search reuslts GridView1.DataSource = SortDataTable(Search() as DataTable, true); GridView1.DataBind(); } #endregion #region Properties SortDirection and SortExpresssion /// <summary> /// property GridView Sort Direction /// </summary> private string GridViewSortDirection { get { return ViewState["SortDirection"] as string ?? "ASC"; } set { ViewState["SortDirection"] = value; } } /// <summary> /// GridView sort expression /// </summary> private string GridViewSortExpression { get { return ViewState["SortExpression"] as string ?? string.Empty; } set { ViewState["SortExpression"] = value; } } #endregion #region SortDirection method /// <summary> /// Get current sortDirection and switch between /// </summary> /// <returns></returns> private string GetSortDirection() { switch (GridViewSortDirection) { case "ASC": GridViewSortDirection = "DESC"; break; case "DESC": GridViewSortDirection = "ASC"; break; } return GridViewSortDirection; } #endregion #region SortDataTable /// <summary> /// Sort Data Table when Page Index changing /// </summary> /// <param name="dataTable"></param> /// <param name="isPageIndexChanging"></param> /// <returns>sorted DataView</returns> protected DataView SortDataTable(DataTable dataTable, bool isPageIndexChanging) { if (dataTable != null) { DataView dataView = new DataView(dataTable); if (GridViewSortExpression != string.Empty) { if (isPageIndexChanging) { dataView.Sort = string.Format("{0} {1}",
GridViewSortExpression, GridViewSortDirection); } else { dataView.Sort = string.Format("{0} {1}",
GridViewSortExpression, GetSortDirection()); } } return dataView; } else { return new DataView(); } } #endregion #region GridView columns sorting /// <summary> /// Handle page sorting /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { //Get sortExpression GridViewSortExpression = e.SortExpression; int pageIndex = GridView1.PageIndex; //Bind GridView with search results GridView1.DataSource = SortDataTable(Search() as DataTable, false); GridView1.DataBind(); //Set the page Index pageIndex = GridView1.PageIndex; } #endregion
References