Manually sorting and paging Gridview without using datasource control

Hi

Many a times while working with Gridview we want to work with the paging and sorting functionality without using any datasource control. Gridview is flexible enough to perform these tasks without the use of any datasource control and only a few lines of code.  

For paging a Gridview manually we have to handle the Gridview’s PageIndexChanged Event.  In the event we need to change the PageIndex of Gridview to the page that user has clicked and again bind the Gridview.

protected void gvPaging_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
   gvPaging.PageIndex = e.NewPageIndex;
   gvPaging.DataBind();
}

This is all you need to do to make manual paging in the Gridview.

For manually sorting in Gridview we need to handle the Sorting event of Gridview. Here is the code to do it.

protected void gvSorting_Sorting(object sender, GridViewSortEventArgs e)
{
   DataTable dtSortTable = gvSorting.DataSource as DataTable;

   if (dtSortTable != null)
   {
      DataView dvSortedView = new DataView(dtSortTable);
     dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection);

      gvSorting.DataSource = dvSortedView;
      gvSorting.DataBind();
   }
}

private string getSortDirectionString(SortDirection sortDireciton)
{
   string newSortDirection = String.Empty;
if(sortDirection== SortDirection.Ascending)
{
       newSortDirection = "ASC";
}
else
{
       newSortDirection = "DESC";
}

 return newSortDirection
}

And we have the manual sorting for the Gridview.

Vikram

24 Comments

  • You have some assumptions. First of all that the data is still there after a roundtrip (postback). Doing manual paging is done for only one reason and that is preventing extra data transmitted in the viewstate so that the gridview, etc. do not have to use the viewstate to hold all data. This can build up easiliy if you do not limit the rows returned from your datasource. Doing paging for 10000 while displaying only 20 will still transmit all other rows.

  • Hi Thank you for the nice code

    If we have 20 records and page size is 10

    then shall this code will going to sort all 20 records??

  • Hi purvesh.shah

    yes the code will sort all the 20 records.

  • DataTable dtSortTable = gvSorting.DataSource as DataTable;
    this statement is working.Datatable is showing null value

  • If i want to sort only 1st 10 records of page how can i do?

  • datatable = TryCast(grdsorting.DataSource, DataTable)
    In this statement grdsorting.DataSource showing Nothing.
    what i do
    plz help me.
    Thanks

  • Hi
    After data are sorted
    then i go on next page.
    At that time i sort data Descending order
    now i click on next page on paging it will give data from ascending order.
    n i want that data from descending oreder
    means
    i want 2nd page from descending order.
    Thanx.
    Hace a nice day.

  • protected void gvPaging_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
    gvPaging.PageIndex = e.NewPageIndex;
    gvPaging.DataBind();
    }

    This doesn't work when cache is not set. Ramon Smits is correct - data is not there after the round trip. GridView is set to null - no data displayed.

  • Hi
    i am using the code for paging
    but is notwoking , when i click on link of next page
    records from gidview disappear.
    can u tell me why it is happening.
    Thanks
    john

  • You have a couple of typos in your code above:

    private string getSortDirectionString(SortDirection sortDireciton) should be:
    private string getSortDirectionString(SortDirection sortDirection)

    and

    return newSortDirection should be:
    return newSortDirection;

  • Anyone know how to do this in VB?

    Thanks

    Chris.

  • I used the code for paging but when i select the next page the Grid disappears. Can anybody Help me?

  • Corrected function below (1 spelling mistake and a missing semi-colon). Thanks for this, Vikram. Helped me quite a bit :)

    private string getSortDirectionString(SortDirection sortDirection)
    {
    string newSortDirection = String.Empty;
    if(sortDirection== SortDirection.Ascending)
    {
    newSortDirection = "ASC";
    }
    else
    {
    newSortDirection = "DESC";
    }
    return newSortDirection;
    }

  • Hi,

    Thanx for giving the code for sorting & paging.......
    But the problem now i'm facing is abt Descending order sorting......
    can u help me to solve that problem also.....

    thanx in advance

  • I never saw a reply to the queries about the grid disappearing when a new page is selected. How does one get the grid to stay put and not disappear when paging?

  • Thank You for this it was very helpful. It sounds like some people here want to do nothing but copy and paste. The info he gave is more then enough to do this. If you need extra help then maybe programming isn't for you.

  • Friday, December 05, 2008 10:21 AM by RS3
    //Work only if the gridview is bound to a SqlDataSource or ObjectDatasource
    gvPaging.PageIndex = e.NewPageIndex;
    gvPaging.DataBind();


    OR

    gvPaging.PageIndex = e.NewPageIndex;
    gvPaging.DataSource =
    gvPaging.DataBind();

  • Hey it worked for me...
    but u know... the records are not in the viewstate...
    i had to bind it again to the DataSet and then when i set the pageIndex ... it worked..
    does somebody has a better idea to do this... without resettin the DataSource Property..?????

  • DataTable dtSortTable = gvSorting.DataSource as DataTable;

    Bad example... this line will always break

  • thanks for this. this is similar to msdn tutorial anyway...

  • hey in ur code descending order sortin aint happening...y???

  • If I click a column on the gridview, it sorts it in ASC (Ascending) order. I would expect that when I click the same column again, it should sort in DESC (Descending) order. However, it keeps sorting in Ascending order. The incoming GridViewSortEventArgs argument shows an Ascending sort all the time. Does this ever switch to Descending or do you have to check the sort on the dataview and manipulate it to do descending sorts?

  • I resolved the descending sort issue as follows:

    protected void gridviewResults_Sorting(object sender, GridViewSortEventArgs e)
    {

    DataView dataView = (DataView)ViewState[constDataSource];

    if (dataView != null)
    {
    string strSortDirection = e.SortDirection == SortDirection.Ascending ? "ASC" : "DESC";
    if (e.SortExpression + " " + strSortDirection == dataView.Sort)
    {
    dataView.Sort = e.SortExpression + " " + (e.SortDirection == SortDirection.Ascending ? "DESC" : "ASC");
    }
    else
    {
    dataView.Sort = e.SortExpression + " " + strSortDirection;
    }
    gridviewResults.DataSource = dataView;
    gridviewResults.DataBind();
    ViewState[constDataSource] = dataView;
    }

  • the sorting code is not working please help .
    please mail me the right code .plzzzzz
    my code is as follows
    ------------------------------------------------------

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;

    public partial class gridExManual : System.Web.UI.Page
    {
    Data data;
    DataSet ds;
    string query;
    protected int sno;
    protected void Page_Load(object sender, EventArgs e)
    {
    data = new Data();
    showGrid();
    }
    private void showGrid()
    {
    query = "SELECT id as SNO, Name, Address, Dob, Gender, Qualification, Course FROM Registration";
    ds = data.getDataSet(query);
    GridView1.DataSource = ds;
    GridView1.DataBind();

    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
    //sno = GridView1.PageSize * e.NewPageIndex;
    GridView1.PageIndex = e.NewPageIndex;
    //showGrid();
    GridView1.DataBind();

    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
    DataTable dtSortTable = GridView1.DataSource as DataTable;

    if (dtSortTable != null)
    {
    DataView dvSortedView = new DataView(dtSortTable);
    dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection);

    GridView1.DataSource = dvSortedView;
    GridView1.DataBind();
    }
    }

    private string getSortDirectionString(SortDirection sortDirection)
    {
    string newSortDirection = String.Empty;
    if (sortDirection == SortDirection.Ascending)
    {
    newSortDirection = "ASC";
    }
    else
    {
    newSortDirection = "DESC";
    }

    return newSortDirection;
    }
    }


    -----------------------------------------------------------

Comments have been disabled for this content.