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

2 Comments

  • hello Sukumar,

    I have been looking for a way to enable Sorting in a GridView Bound to a Custom Data Object. I tryed to use your article, but the article is incomplete, I can only read part of the code, but no explanation. And the code is also incomplete. Please if it is possible to you, can you send me a copy of the article and the code to my email account (minoguanito@hotmail.com)

    Thanks,


  • Hi Hector Rodriguez,

    The source is self explanatory. I try to comment where it better explains the concept.

    Let me know where you come across problems with the source code. So that a resolution can be suggested.

Comments have been disabled for this content.