Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How add page size in gridview in code behind
Posted

Simply by setting pagesize property of the respective GridView. E.g. if you have a gridview with id as grdVw1 then you can set the page size in code behind as:

C#
grdVw1.pagesize = 10;


you will need to rebind the gridview in order for this change to take place.
 
Share this answer
 
Try this:
C#
GridView1.DataSource = MyDataSource; //This should be replaced with your datasource
GridView1.EnableViewState = true;
GridView1.AllowPaging = true;
GridView1.PageSize = 4;
GridView1.DataBind();

You can also fire PageIndexChanging event. E.g.:
C#
GridView1.PageIndexChanging += new GridViewPageEventHandler(GridView1_PageIndexChanging);

Code to handle page index change event:
C#
void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
   //Your code block
}



--Amit
 
Share this answer
 
v2
Comments
Rekhash 9-Apr-13 7:37am    
@_Amy it cannot able to dispaly in the second page in gridview...
_Amy 9-Apr-13 7:40am    
If you are binding your gridview in page_load event then bind it in Page !IsPostBack block.
You need to rebind the GridView and set the NewPageIndex in PageIndexChanging event . e.g.:
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = MyDataSource;
GridView1.DataBind();


--Amit
Rekhash 9-Apr-13 7:49am    
@_Amy
it is not coming...
_Amy 9-Apr-13 7:53am    
GridView1.PageIndexChanging += new GridViewPageEventHandler(GridView1_PageIndexChanging);
should be there after binding the grid. Where you are writing it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900