Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Gridview paging example in asp.net with Numeric, NextPrevious, NextPreviousFirstLast, NumericFirstLast

Mar 8, 2011
Introduction:

Here I will explain how to show the gridview with different type of paging in asp.net.

Description:


I have one gridview with multiple number rows at that time I tried to set paging for my gridview for that I have search gridview properties at that time I found different type of paging mode styles are available in gridview for paging those are

1.      1) Numeric
2.      2) NextPrevious
3.      3) NextPreviousFirstLast
4.      4) NumericFirstLast
To use these options in our gridview we need to set AllowPaging="true" and PageSize="5" Properties here pageSize you can change based on your requirement after set these properties we can use different type of pagerstting modes to show different type of paging for gridview.

First Design our aspx page like this


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Show Gridview To</title>
<style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:11pt;
font-weight:normal;
color:black;
width:350px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvEmpDetails" runat="server" CssClass="Gridview" AllowPaging="true" PageSize="4" HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White" DataSourceID="dsEmpdetails">
<PagerSettings Mode="NumericFirstLast" PageButtonCount="4"  FirstPageText="First" LastPageText="Last"/> 
<PagerStyle BackColor="#7779AF" Font-Bold="true" ForeColor="White" /> 
</asp:GridView>
<asp:SqlDataSource ID="dsEmpdetails" runat="server" ConnectionString="<%$ConnectionStrings:dbconnection %>"
SelectCommand="select * from EmployeeDetails" >
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
After that set your database connection in web.config like this because we are using this connection in our sqldatasource to get the data from database

<connectionStrings>
<add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"/>
</connectionStrings >


Now our gridview is ready with data and I will explain how to use each paging option in our gridview if you observe above code I have set pagersetting property for gridview like this

<PagerSettings Mode="NumericFirstLast" PageButtonCount="4"  FirstPageText="First" LastPageText="Last"/>
Here I used NumericFirstLast pagersetting mode to display paging in gridview

NumericFirstLast: By setting this mode in gridview paging is like this 

Here in this paging mode PageButtonCount property is important because if we need to see the first and last pager buttons we need to reduce the PageButtonCount value because whenever our gridview paging data more than 10 pages then only we will see the Last and First buttons that is default value for gridview that’s why I have reduced my PageButtonCount value to 4 after 4 numeric pagers we will see Last button.      
  
Numeric: By default we will get this numeric paging once we set AllowPaging and PageSize properties in gridview to set this property to our gridview write the pagersetting property like this

<PagerSettings Mode="Numeric" PageButtonCount="4" />
By Setting above pager property our gridview paging like this


NextPrevious: If we want to use this mode for paging in gridview we need to change the PagerSettings mode like this

<PagerSettings Mode="NextPrevious" PageButtonCount="4" PreviousPageText="Previous" NextPageText="Next" /> 
After changing PagerSettings mode our gridview like this


NextPreviousFirstLast: If we want to use this mode for paging in gridview we need to change the PagerSettings mode like this

<PagerSettings Mode="NextPreviousFirstLast" PageButtonCount="4" PreviousPageText="Previous" NextPageText="Next" FirstPageText="First" LastPageText="Last" />
After changing PagerSettings mode our gridview like this

Up to now we used the gridview available features suppose if we want to show the gridview with custom paging like Numeric,first,previous,next and last in gridview we need to write the separate code for this because we don't have built in feature of gridview check this link here i explained how to show the gridview with custom paging like numeric,first,previous,next,last


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

29 comments :

ajay dubey said...

very fine code sir!
thanks sir!
I have very need for this code.

Anonymous said...

good post

Anonymous said...

i use this code but get Error.
The GridView 'grdPurPlan1' fired event PageIndexChanging which wasn't handled.

Suresh Dasari said...

hi,
check your code that was problem with your coding if you observe above code i didn't used that type of events. i thing you written event in aspx page but forgot to handle that event in code behind check your code.

Anonymous said...

Very very nice code.

Anonymous said...

Good one :) Thank you

Navjot Singh said...

hi suresh,

i have a problem may be you have any idea.
i want to use gridview with paging and i want when my records are shown then only next button should show in footer of gridview but when user reach on the last record then there should show "Last" that means before this last should not be show.

please give me solution.

my emailid is hello_navjot@yahoo.com

Anonymous said...

but i want gridview without setting paging=true

Jorge said...

Hi Suresh. I am new at this gridview control. Your article is very useful. Thanks for sharing!

Hazrat said...

Hi sir, I have a problem,how to solve this, without sqldatasource, (using templated feild, edit item templated, item templated, in a gridview) how to use customPaging style....I know databinding without sqldatasource,Plz give me the tips how can i solve that one when i m using Templated feild...Thanks

Hazrat said...

can anyone help me to the above post plz. thanks in adv

Anonymous said...

its not working

Anonymous said...

awesome, thx for sharing!

Durga said...

thank you

athiq said...

hai,,can u send me the code behind for numeric first-last in gridview using c# coding

Sumeet said...

is there any provision to put "Next" and "First" labels at left hand side and "Previous" and "Last" labels on right hand side?

Anonymous said...

u can find better code than this on google

V.Mahesh said...

Your website is very useful for .net programmers. Thank uuuuu

Anonymous said...


http://blog.bhansalisoft.com/Posts.aspx?PostID=129

Gurmeet said...

how we done paging on conditional based
suppose i have a 3 column and last column we have yes or no.i bind griddview on this condition.
in this case error occur

Anonymous said...

Keep Poasting , You are posting a good material here that helps me alot.

Unknown said...

can you please give example of gridview custom paging with large amount of data.

Janu said...

In My Gridview page Error Will show like this "the gridview fired event pageindexchanging which wasn't handled" please help me sir...

Anonymous said...

how to understand asp.net sir....sometimes it becomes very difficult to understand that what to code....

Unknown said...

its very good article:
i am developing a application in which i am stucked,
i want to query on users and friends
i want to fetch freindsdata from users table
i have table friend with field:
Id fromid toid status
1 1 2 1

and in usertable fields:
RankId Fullname Imagepath
1 kk kk.jpg

where some one send request and other one accepted it goes in friend table
it should insert in anyone column of fromid toid
it depends on who send request and who accept request
after that
when i login i want to fetch my freind data
help me

Anonymous said...

provide the demos pictures for tasks

Unknown said...

hey suresh .how can i set space between First next Last Previous Button.?

Unknown said...

Can't perform Create, Update, or Delete operations on 'Table(tblfee)' because it has no primary key.
sir this Error is necessary to use primary key but i want to not use tis

Anonymous said...

^^ use &nbsp for space

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.