locked
GridView Paging Index binging data for first page only RRS feed

  • Question

  • User198251766 posted

    I'm hoping to get pointed in the right direction after scouring the internet for hours trying to find my solution. My gridview is only showing me the data for the first page of the paging index. When I click on any other pages none other rows show up. Here's my gridview code:

    <asp:GridView ID="grdViewPersonnel" runat="server" CssClass="datagrid" CellPadding="4"
                    BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
                    PageSize="20" AutoGenerateColumns="False" RowStyle-Height="30px" ForeColor="Black"
                    AllowPaging="True" OnPageIndexChanging="grdViewPersonnel_PageIndexChanging" EnableSortingAndPagingCallbacks="True"
                    ShowHeaderWhenEmpty="True">

    Here is the code behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class frmViewInvoices : System.Web.UI.Page
    {
    
    
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();
            clsDataLayer.UpdateDates(Server.MapPath("PayrollSystem_DB.mdb"));
            
            if (!Page.IsPostBack)
            {  
             string strSearch = (string)Session["txtSearchName"];
    
                // Declares the DataSet
                dsPersonnel myDataSet = new dsPersonnel();
                myDataSet = clsDataLayer.GetPersonnel(Server.MapPath("PayrollSystem_DB.mdb"), strSearch);
                grdViewPersonnel.DataSource = myDataSet.Tables["tblPersonnel"];
                grdViewPersonnel.DataBind();
    
                Session.Remove("txtSearchName"); 
                
            }
    
        }
    
    
        protected void grdViewPersonnel_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
           
           grdViewPersonnel.PageIndex = e.NewPageIndex;
           grdViewPersonnel.DataBind();
            
          
    
        }
        
    }
    

    585
    Wednesday, August 19, 2015 7:11 PM

Answers

  • User1577371250 posted

    Hi,

    try this

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class frmViewInvoices : System.Web.UI.Page
    {
    
    
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();
            clsDataLayer.UpdateDates(Server.MapPath("PayrollSystem_DB.mdb"));
            
            if (!Page.IsPostBack)
            {  
                BindData();
                
            }
    
        }
    
    
        protected void grdViewPersonnel_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
           
           grdViewPersonnel.PageIndex = e.NewPageIndex;
            BindData();
        }
    
     public void BindData()
     {
            string strSearch = (string)Session["txtSearchName"];
    
                // Declares the DataSet
                dsPersonnel myDataSet = new dsPersonnel();
                myDataSet = clsDataLayer.GetPersonnel(Server.MapPath("PayrollSystem_DB.mdb"), strSearch);
                grdViewPersonnel.DataSource = myDataSet.Tables["tblPersonnel"];
                grdViewPersonnel.DataBind();
    
                Session.Remove("txtSearchName"); 
        
     }
        
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 20, 2015 2:33 AM

All replies

  • User1577371250 posted

    Hi,

    try this

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class frmViewInvoices : System.Web.UI.Page
    {
    
    
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();
            clsDataLayer.UpdateDates(Server.MapPath("PayrollSystem_DB.mdb"));
            
            if (!Page.IsPostBack)
            {  
                BindData();
                
            }
    
        }
    
    
        protected void grdViewPersonnel_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
           
           grdViewPersonnel.PageIndex = e.NewPageIndex;
            BindData();
        }
    
     public void BindData()
     {
            string strSearch = (string)Session["txtSearchName"];
    
                // Declares the DataSet
                dsPersonnel myDataSet = new dsPersonnel();
                myDataSet = clsDataLayer.GetPersonnel(Server.MapPath("PayrollSystem_DB.mdb"), strSearch);
                grdViewPersonnel.DataSource = myDataSet.Tables["tblPersonnel"];
                grdViewPersonnel.DataBind();
    
                Session.Remove("txtSearchName"); 
        
     }
        
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 20, 2015 2:33 AM
  • User198251766 posted

    Lokesh-

    Thanks for the reply, in theory that code makes perfect sense, unfortunately my problem is still present

    I even tried adding my data source before the bind data:

    protected void grdViewPersonnel_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            dsPersonnel myDataSet = new dsPersonnel();
            
            grdViewPersonnel.PageIndex = e.NewPageIndex ;
            grdViewPersonnel.DataSource = myDataSet.Tables["tblPersonnel"];
            BindData();
        }

    and no success, any other suggestions?

    154
    Thursday, August 20, 2015 8:59 AM
  • User1577371250 posted

    Hi,

    I don;t see any problem with the code. 

    You can avoid calling the BindData and call DataBind() on gridview.

    protected void grdViewPersonnel_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            dsPersonnel myDataSet = new dsPersonnel();
            
            grdViewPersonnel.PageIndex = e.NewPageIndex ;
            grdViewPersonnel.DataSource = myDataSet.Tables["tblPersonnel"];
            grdViewPersonnel.DataBind();
        }

    Check the sample here

    http://www.aspsnippets.com/Articles/Paging-in-ASPNet-GridView-Example.aspx

    Thursday, August 20, 2015 9:04 AM
  • User198251766 posted

    Lokesh,

    Thanks for your help, I not sure why it didn't work so my work around was to take off the paging on the gridview

    26
    Thursday, August 20, 2015 11:12 PM