2

I have a GridView where I have to show top 200 records on page load and I have set the pagesize="200" in the design. But when I give this property Paging doesn't show in the GridView and when I remove the pagesize="200" property the paging shows up.

The actual case is when I remove the pagesize="200" I am only able to see only 10 records instead of 200 even though my DataTable returns 200 records.

I have also enabled AllowPaging="true".

Can anyone please suggest an alternate or any inputs? It is much appreciated.

HTML Code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" ShowHeaderWhenEmpty="true" EmptyDataText="No Records Found" OnRowDataBound="GridView1_RowDataBound" AllowSorting="true" OnSorting="GridView1_Sorting" OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="true" CellPadding="1" PageSize="200" CellSpacing="1" BackColor="#e7e7e8" BorderColor="#e7e7e8" GridLines="Both" CssClass="GridViewStyleB" Font-Names="Calibri" Font-Size="10pt"> <PagerSettings Mode="NumericFirstLast" PageButtonCount="4" FirstPageText="First" LastPageText="Last" Position="Top" Visible="true" /> <PagerStyle BackColor="#e7e7e8" ForeColor="Black" HorizontalAlign="left" /> <HeaderStyle CssClass="RowStyle" ForeColor="Black" BackColor="#a9a9a9" Font-Underline="false" HorizontalAlign="Left" /> <RowStyle CssClass="RowStyle" HorizontalAlign="Left" BorderColor="#E7E7E8" ForeColor="Black" /> <AlternatingRowStyle CssClass="AlternatingRowStyle" BorderColor="#E7E7E8" ForeColor="Black" BackColor="#e7e7e8" /> <EmptyDataRowStyle HorizontalAlign="Center" /> </asp:GridView> 

Code Behind:

protected void GridView1_PreRender(object sender, EventArgs e) { GridView gv = (GridView)sender; GridViewRow pagerRow = (GridViewRow)gv.TopPagerRow; GridView1.VirtualItemCount = totalRecords; if (pagerRow != null && pagerRow.Visible == false) pagerRow.Visible = true; } 

Update1:

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { GridView1.TopPagerRow.Visible = true; int totalRecords = DAL.GetRecordCount(); GridView1.VirtualItemCount = totalRecords; } } 
8
  • could you please show your gridview html code? Commented Jan 4, 2018 at 13:38
  • @Asif.Ali - Updated html code. Commented Jan 4, 2018 at 13:42
  • try to add AutoPostBack="true" attribute to your gridview. Commented Jan 4, 2018 at 13:44
  • 1
    You said that your database returns 200 records. If you set pagesize=200 and you have only 200 records, you only have 1 page. Commented Jan 4, 2018 at 13:49
  • @Asif.Ali - I added AutoPostBack="true" and still paging isn't visible. Commented Jan 4, 2018 at 13:49

1 Answer 1

1

Since you only have one page, you need to "force" it. Try it:

GridView1.BottomPagerRow.Visible=true GridView1.VirtualItemCount = totalRecords; 

And also change the property AllowCustomPaging: http://www.c-sharpcorner.com/UploadFile/99bb20/custom-paging-with-gridview-control-in-Asp-Net-4-5/

Sign up to request clarification or add additional context in comments.

7 Comments

This works! But I'm able to see only 1 in the pager. For ex: If the table count is 1000 then 1000/200 = 5. So I need to have these 5 page numbers shown in the pager. I'm trying to achive this. So when anyone clicks on any particular page then I will handle them in pageindex event.
I have updated my code in the post and is this correct way and I handle them in prerender event?
You should set this property before you bind the data to your grid, not in the prerender. I just remember you also need to set the property AllowCustomPaging. I'll update my answer.
I have updated my post with the changes and pager didn't yet show up.
It looks wrong, you should set these properties just before setting the DataBinding, not inside prerender. Take a look at the url I posted.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.