CJ

The circle of life – ** .Net – Training – MOSS **

  • Past Post

  • Vistor Locations

Paging and Sorting GridView Control

Posted by Clayton James on October 19, 2009

If you implement paging and sorting in a GridView control then you need to maintain column level sorting when you move between pages.

I apoligise for the code formatting but wordpress doesn’t format code blocks very well.

General Methods
//
general method to bind grid. Pass in true if this method is called from the page index event
protected void GridBind(bool IsPageIndexEvent)
{
    GridRequests.DataSource = SortDataTable(GetRequestsDataTable(LoginCode), IsPageIndexEvent);
    GridRequests.DataBind();
}

 //get the data from the database and return a DataTable
protected DataTable GetRequestsDataTable(string LoginCode)
{
    Requests business =
new Requests();
    return business.GetUsersRequests(LoginCode).Tables[0];
}

//sort the DataTable and maintain sort direction if called from page index event
 protected DataView SortDataTable(DataTable dataTable, bool isPageIndexChanging)
{
    if (dataTable != null)
    {
        DataView dataView =
new DataView(dataTable);
        if (GridViewSortExpression != string.Empty)
        {
            if (isPageIndexChanging) //pass in true if event fired from pageindex event
            dataView.Sort =
string.Format(“{0} {1}”, GridViewSortExpression, GridViewSortDirection);
        else
            dataView.Sort = string.Format(“{0} {1}”, GridViewSortExpression, GetSortDirection());
        }
        return dataView;
    }
    else
        return new DataView();
}
 
Sorting
Set the AllowSorting property to ‘true’ for the GridView
Set a couple of properties to maintain sort column and direction across postbacks
 
private string GridViewSortDirection
{
    get { return ViewState[“SortDirection”] as string ?? “ASC”; }
    set { ViewState[“SortDirection”] = value; }
}

private string GridViewSortExpression
{
    get { return ViewState[“SortExpression”] as string ?? “Request Id”; }
    set { ViewState[“SortExpression”] = value; }
}

//this method is called to alternate sort direction
private string GetSortDirection()
{
    switch (GridViewSortDirection)
    {
        case “ASC”:
            GridViewSortDirection = “DESC”;
            break;
        case “DESC”:
            GridViewSortDirection =
“ASC”;
            break;
    }
    return GridViewSortDirection;
}

 
//alternate sort direction and maintain page index
void GridRequests_Sorting(object sender, GridViewSortEventArgs e)
{
    GridViewSortExpression = e.SortExpression;
    int pageIndex = GridRequests.PageIndex;
    GridBind(
false); //pageindex event did not fire binding
    GridRequests.PageIndex = pageIndex;
}

 
 
Turn paging on in the GridView control and wire up the Grid_PageIndexChanging eventprotectedvoid GridRequests_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridBind(
true);//maintain sort direction as we are only navigating between pages
    GridRequests.PageIndex = e.NewPageIndex;
    GridRequests.DataBind();
}
Advertisements

6 Responses to “Paging and Sorting GridView Control”

  1. I like the article, though am struggling a bit to implement simialr functionality on a page I’m working on… After sorting, I am left with an empty resultset.

    Do I need to call GridBind(false) or something to that effect on Page_Load()? Page_Init()?

    They sure don’t make it easy to handle concurrent paging and sorting, do they? 😉

    R/
    Brian Dobberteen
    http://brian.dobberteen.com

    P.S., check out the SyntaxHighlighter plugin for wordpress. You can see an example of it at my blog at the above link.

  2. Actually just got it sorted… I am not smart.

    I am generating my gridview from search terms provided by the user, and forgot to call BindGrid() in the Search button’s click event handler.

    Putting GridBind(bool) in Page_Load is not recommended 😉

  3. Sorry, meant to say GridBind(bool) not BindGrid().

  4. CJ said

    Hi Brian
    Great to hear you got it sorted out.

    I can’t install the plugin as WP host my blog and they don’t support this plugin.

  5. Ariel said

    Excelent solution, thanks!

  6. Nice post. I learn something totally new and challenging on websites I stumbleupon every day.
    It will always be helpful to read articles from other writers and practice a
    little something from other web sites.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: