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
Published Tuesday, April 15, 2008 5:39 AM by vik20000in
Filed under: , ,

Comments

# re: Manually sorting and paging Gridview without using datasource control

Tuesday, April 15, 2008 3:36 AM by Ramon Smits

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.

# Manually sorting and paging Gridview without using datasource control

Tuesday, April 15, 2008 8:48 AM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# re: Manually sorting and paging Gridview without using datasource control

Tuesday, April 15, 2008 9:07 AM by purvesh.shah

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??

# re: Manually sorting and paging Gridview without using datasource control

Thursday, April 17, 2008 7:59 AM by vik20000in

Hi purvesh.shah

yes the code will sort all the 20 records.

# re: Manually sorting and paging Gridview without using datasource control

Thursday, May 08, 2008 7:30 AM by Sanjay

DataTable dtSortTable = gvSorting.DataSource as DataTable;

this statement is working.Datatable is showing null value

# re: Manually sorting and paging Gridview without using datasource control

Tuesday, May 13, 2008 5:08 AM by itsvineeth209

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

# re: Manually sorting and paging Gridview without using datasource control

Monday, May 19, 2008 2:27 AM by sima

datatable = TryCast(grdsorting.DataSource, DataTable)

In this statement grdsorting.DataSource showing Nothing.

what i do

plz help me.

Thanks

# re: Manually sorting and paging Gridview without using datasource control

Monday, May 19, 2008 3:23 AM by sima

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.

# re: Manually sorting and paging Gridview without using datasource control

Friday, June 20, 2008 9:47 PM by nic.w

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.

# re: Manually sorting and paging Gridview without using datasource control

Friday, August 15, 2008 8:09 AM by john

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

# re: Manually sorting and paging Gridview without using datasource control

Saturday, September 13, 2008 4:37 PM by Scott M

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;

# re: Manually sorting and paging Gridview without using datasource control

Wednesday, September 17, 2008 5:10 AM by westicle

Anyone know how to do this in VB?

Thanks

Chris.

# re: Manually sorting and paging Gridview without using datasource control

Tuesday, November 04, 2008 10:35 AM by Rickey

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

# re: Manually sorting and paging Gridview without using datasource control

Wednesday, November 26, 2008 10:26 AM by Merwan

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;

}

# re: Manually sorting and paging Gridview without using datasource control

Thursday, December 04, 2008 12:31 AM by Rahul

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

# re: Manually sorting and paging Gridview without using datasource control

Thursday, December 04, 2008 3:59 AM by cherishnews

Hi,

Thank you for the article.

I am using a similar code in VB.

But I'm stuck at DataTable dtSortTable = gvSorting.DataSource as DataTable

Seems like the DataSource becomes Nothing after the search (result displayed well on gridview though)

Do you have any ideas what might be wrong?

Thank you!

# re: Manually sorting and paging Gridview without using datasource control

Friday, December 05, 2008 10:21 AM by RS3

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?

# re: Manually sorting and paging Gridview without using datasource control

Friday, January 02, 2009 2:01 PM by John

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.

# re: Manually sorting and paging Gridview without using datasource control

Sunday, February 08, 2009 11:35 PM by Anonymous

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 = <Get the data from the database in a dataset or datatable>

gvPaging.DataBind();

# re: Manually sorting and paging Gridview without using datasource control

Tuesday, March 10, 2009 6:16 AM by SYED MUTAHAR AUQIB

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..?????

# GridView Sorting Event Handler Called Twice on one OnSorting Event &laquo; Bite The Wax Tadpole

Pingback from  GridView Sorting Event Handler Called Twice on one OnSorting Event &laquo; Bite The Wax Tadpole

# re: Manually sorting and paging Gridview without using datasource control

Thursday, March 26, 2009 3:46 PM by Erik

DataTable dtSortTable = gvSorting.DataSource as DataTable;

Bad example... this line will always break

# re: Manually sorting and paging Gridview without using datasource control

Monday, May 11, 2009 3:15 PM by Squee G

These examples don't take into account multiple columns. MSDN has an official working example:

msdn.microsoft.com/.../system.web.ui.webcontrols.gridview.sorting.aspx

# re: Manually sorting and paging Gridview without using datasource control

Thursday, July 23, 2009 4:54 AM by Rana

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

# re: Manually sorting and paging Gridview without using datasource control

Thursday, November 05, 2009 1:40 AM by dhavalshah

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

# re: Manually sorting and paging Gridview without using datasource control

Friday, November 20, 2009 1:28 PM by motor955

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?

# re: Manually sorting and paging Gridview without using datasource control

Friday, November 20, 2009 1:49 PM by motor955

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;

       }

Leave a Comment

(required) 
(required) 
(optional)
(required)