Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
In one of Gridview I want to show only 3 records per page. I tried with the following code but not working as expected. It shows 5 rows instead.
SQL
<asp:GridView ID="GridView3" runat="server" HorizontalAlign="Center"  AutoGenerateColumns="False"
                style="border-color: #808000; top: 450px; left: 515px; position: absolute; height: 84px; width: 384px"
                PageSize="3"
                onselectedindexchanged="GridView3_SelectedIndexChanged">
Posted
Updated 20-Apr-16 22:50pm

VB
<asp:gridview id="GridView3" runat="server" horizontalalign="Center" autogeneratecolumns="False" style="border-color: #808000; top: 450px; left: 515px; position: absolute; height: 84px; width: 384px" pagesize="3" allowpaging="true" onpageindexchanging="GridView3_PageIndexChanging" onselectedindexchanged="GridView3_SelectedIndexChanged" xmlns:asp="#unknown">
 </asp:gridview>


Implement pageindex changing as below

C#
protected void GridView3_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
   /*Fill Gridview*/
   GridView3.PageIndex = e.NewPageIndex;
   GridView3.DataBind();
}
 
Share this answer
 
PageSize Property is normally used for the functionality you required. but if it is not working than do check the aspx page for Java Script Issue in case you are using JS on the page.. and if not than right click on the gridview and select properties and there change the PageSize property to 3... as I have noticed that sometimes Visual Studio 2010 didnt imply the properties if written in source... :(

Hope it will solve your problem.

Regards.
 
Share this answer
 
PageSize is the property that can be used to set the number of records per page. From your code given above, I see that it is mentioned. However you also need to set, AllowPaging = "true" property of the gridview. See if it works after setting this property. Reply back if it does not.
 
Share this answer
 
 
Share this answer
 
Use attribute AllowPaging="True" in gridview

C#
<asp:gridview id="GridView3" runat="server" horizontalalign="Center" autogeneratecolumns="False" xmlns:asp="#unknown">
                style="border-color: #808000; top: 450px; left: 515px; position: absolute; height: 84px; width: 384px" AllowPaging="True" 
                PageSize="3"
                onselectedindexchanged="GridView3_SelectedIndexChanged"></asp:gridview>
 
Share this answer
 
v2
set page size in grid view source code or in property like

<asp:gridview id="GridView" runat="server" autogeneratecolumns="False" >
PageSize="3"


i think it helps u..
 
Share this answer
 
v2
<asp:gridview id="yourgridview" runat="server" horizontalalign="Center" autogeneratecolumns="False" style="border-color: black; top: 450px; left: 500px; position: absolute; height: 70px; width: 360px" pagesize="3" allowpaging="true" onpageindexchanging="yourgridview_PageIndexChanging" onselectedindexchanged="yourgridview_SelectedIndexChanged" xmlns:asp="#unknown">


page index handling:

protected void yourgridview_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
/*code for populating gridview goes here*/
yourgridview.PageIndex = e.NewPageIndex;
yourgridview.DataBind();
}
 
Share this answer
 
Step 1. AllowPaging = "false" and PageSize ="3"
Step 2. In the code

DataSet ds = new DataSet();
ds = obj.StoredProc_Name("param1,param2");
DataTable dt = ds.Tables[0].Rows.Cast().Take(3).CopyToDataTable();
Gridviewname.DataSource = dt;
Gridviewname.DataBind();

Thus suppose if we are getting 10 records from the database then using we are forcing to show only 3 result.
Its simple and working code !...Please write for further clarification.
 
Share this answer
 
v2
Comments
CHill60 21-Apr-16 7:16am    
OP accepted a solution 3 years ago!

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