0

I'm trying to allow paging for my gridview. I have allowpaging at my gridview and also added pagesize. Unfortunately, it doesn't work. I researched and i have seen people not requiring to add any codes.

This is my source code for my gridview

<asp:GridView ID="GVVerify" runat="server" AllowPaging="True" AutoGenerateSelectButton="True" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" CssClass="gridviewadminverify" ForeColor="Black" OnSelectedIndexChanged="GVVerify_SelectedIndexChanged" Width="100%"> <FooterStyle BackColor="#CCCCCC" /> <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" /> <RowStyle BackColor="White" /> <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#F1F1F1" /> <SortedAscendingHeaderStyle BackColor="#808080" /> <SortedDescendingCellStyle BackColor="#CAC9C9" /> <SortedDescendingHeaderStyle BackColor="#383838" /> </asp:GridView> 

This is how i connect my SQL via databinding which is different from using datasource.

SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source = localhost; Initial Catalog = project; Integrated Security= SSPI"; conn.Open(); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter("SELECT policeid as [Police ID], password as [Password], email as [Email], nric as [NRIC], fullname as [Full Name], contact as [Contact], address as [Address], location as [Location] From LoginRegisterPolice where pending='pending'", conn); da.Fill(ds); GVVerify.DataSource = ds; GVVerify.DataBind(); conn.Close(); 

2 Answers 2

1

You are not using PageIndexChanging event and you have to bind PageIndexChanging event and rebind your grid using current page.

Html

<asp:GridView ID="GVVerify" runat="server" OnPageIndexChanging="GridViewPageEventHandler" AllowPaging="True" AutoGenerateSelectButton="True" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" CssClass="gridviewadminverify" ForeColor="Black" OnSelectedIndexChanged="GVVerify_SelectedIndexChanged" Width="100%"> 

Code behind

protected void grdView_PageIndexChanging(object sender, GridViewPageEventArgs e) { GVVerify.PageIndex = e.NewPageIndex; GVVerify.DataSource = GetGridData(); GVVerify.DataBind(); } 
Sign up to request clarification or add additional context in comments.

Comments

0

You need to handle the PageIndexChanging event of the gridview.

for example:

protected void GVVerify_PageIndexChanging(object sender, GridViewPageEventArgs e) { bindGrid(); GVVerify.PageIndex = e.NewPageIndex; GVVerify.DataBind(); } 

where bindGrid method will contain the code for binding the grid. For Example

private void bindGrid() { SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source = localhost; Initial Catalog = project; Integrated Security= SSPI"; conn.Open(); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter("SELECT policeid as [Police ID], password as [Password], email as [Email], nric as [NRIC], fullname as [Full Name], contact as [Contact], address as [Address], location as [Location] From LoginRegisterPolice where pending='pending'", conn); da.Fill(ds); GVVerify.DataSource = ds; GVVerify.DataBind(); conn.Close(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.