Employers - Post Free Jobs
Forum guidelines
  • Home
  • »
  • .NET General
  • »
  • Paging in Gridview not working 2nd page data not showing ?
AuthorQuestion
Paging in Gridview not working 2nd page data not showing ?
Posted on: 28 Mar 2013
here my code
<div style="margin-left: 280px">
            <asp:GridView ID="exportGrdVw" runat="server" BackColor="White"  
        BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
        ForeColor="Black" GridLines="Vertical" AllowPaging="True" PageSize="3" 
                OnPageIndexChanging="exportGrdVw_PageIndexChanging" 
                onpageindexchanged="exportGrdVw_PageIndexChanged">
      protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            SqlDataAdapter da = new SqlDataAdapter("select customername,contactno,address from employee ", con);

            da.Fill(table);

            BindEmployee();
        }
    }
    public void BindEmployee()
    {

        exportGrdVw.DataSource = table;
        exportGrdVw.DataBind();

    }
protected void exportGrdVw_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        exportGrdVw.PageIndex = e.NewPageIndex;
        BindEmployee();
    }
the problem is gridview is displaying but when i click page 2. 2nd page data is not showing (blank).         

AuthorReply
Re: Paging in Gridview not working 2nd page data not showing ?
Posted on: 28 Mar 2013  
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Paging_in_GridView._Default" %> Untitled Page
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; namespace Paging_in_GridView { public partial class _Default : System.Web.UI.Page { string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; SqlCommand com; SqlDataAdapter sqlda; DataSet ds; string str; protected void Page_Load(object sender, EventArgs e) { bindgrid(); } void bindgrid() { SqlConnection con = new SqlConnection(connStr); con.Open(); str = "select * from employee"; com = new SqlCommand(str, con); sqlda = new SqlDataAdapter(com); ds = new DataSet(); sqlda.Fill(ds, "employee"); g1.DataMember = "employee"; g1.DataSource = ds; g1.DataBind(); con.Close(); } protected void g1_PageIndexChanging(object sender, GridViewPageEventArgs e) { g1.PageIndex = e.NewPageIndex; bindgrid(); } } }

If this post helps you mark it as answer
Thanks

Re: Paging in Gridview not working 2nd page data not showing ?
Posted on: 28 Mar 2013  
Hello Friend,
                From my point of view the main problem is with your table variable of DataSet Or DataTable in your code. I think you might have declare at page level like the following.

DataSet ds=new DataSet();

here whenever your page refreshes a new copy of DataSet will be created. and when you select page 2 on GridView that causes a postback, and all the data inside the Dataset is lost That's why you are able to see only blank thing. What you will have to do is the process of filling the data into the DataSet have to be done inside BindEmployee Function.

public void BindEmployee()     {

        SqlConnection con=new SqlConnection("connection string here");
        SqlDataAdapter da=new SqlDataAdapter("Sql Query",con);         da.fill(table); exportGrdVw.DataSource = table; exportGrdVw.DataBind(); }

And now call the function on PageIndexChanging Event of gridview.

Hope that solves your problem.

With Regards,
Vishal Gilbile.

Re: Paging in Gridview not working 2nd page data not showing ?
Posted on: 28 Mar 2013  
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Paging_in_GridView._Default" %> Untitled Page
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; namespace Paging_in_GridView { public partial class _Default : System.Web.UI.Page { string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; DataTable dataTable = new DataTable(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindEmployee(); } } public void BindEmployee() { SqlConnection con = new SqlConnection(connStr); con.Open(); SqlDataAdapter da = new SqlDataAdapter("select * from employee", con); da.Fill(dataTable); g1.DataSource = dataTable; g1.DataBind(); } protected void g1_PageIndexChanging(object sender, GridViewPageEventArgs e) { g1.PageIndex = e.NewPageIndex; BindEmployee(); } } }

If this post helps you mark it as answer
Thanks

SPONSORED BY

Offshore Software and Database Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.