5

my gridview

<div style="margin-left: 280px">
   <asp:GridView ID="exportGrdVw" runat="server" BackColor="White"  
        AllowPaging="True" PageSize="3" 
        OnPageIndexChanging="exportGrdVw_PageIndexChanging" 
        onpageindexchanged="exportGrdVw_PageIndexChanged">
   </asp:GridView>
</div>

my code

SqlConnection con = new SqlConnection("server=acer-Pc\\Sql;database=MYDB;trusted_connection=yes");  
//DataSet ds = new DataSet();
DataTable table = new DataTable();    
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). pls help me to solve this problem. see that the code is correct or not

शेखर
  • 17,412
  • 13
  • 61
  • 117
user2207817
  • 61
  • 1
  • 1
  • 3

4 Answers4

13

Use like below

SqlConnection con = new SqlConnection("server=acer-Pc\\Sql;database=MYDB;trusted_connection=yes");
//DataSet ds = new DataSet();
DataTable table = new DataTable();    
protected void Page_Load(object sender, EventArgs e)
{        
   if (!IsPostBack)
   {              
       BindEmployee();
   }
}
public void BindEmployee()
{
    SqlDataAdapter da = new SqlDataAdapter("select customername,contactno,address from employee ", con);
    da.Fill(table);
    exportGrdVw.DataSource = table;
    exportGrdVw.DataBind();
}
protected void exportGrdVw_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    exportGrdVw.PageIndex = e.NewPageIndex;
    BindEmployee();
}
शेखर
  • 17,412
  • 13
  • 61
  • 117
0

Your datatable gets initialize in every page load so you need to get the datatable everytime before binding it to grid. Set your page Index before binding it to grid

So your code should be like this

public void BindEmployee(int newPageIndex = -1)
{
    SqlDataAdapter da = new SqlDataAdapter("select customername,contactno,address from employee ", con);
    da.Fill(table);
    exportGrdVw.PageIndex = newPageIndex;
    exportGrdVw.DataSource = table;
    exportGrdVw.DataBind();
}
शेखर
  • 17,412
  • 13
  • 61
  • 117
Nishant
  • 306
  • 1
  • 4
0

No need to call database on exportGrdVw_PageIndexChanging. Just declare DataTable table as static

Bharat Jangir
  • 450
  • 1
  • 4
  • 14
-1

You can try this:

GridView1.PageIndex = e.NewPageIndex;
SqlCommand cmd = new SqlCommand("Select * from Emp_Data ORDER BY [ID] DESC", con);

SqlDataAdapter DA1 = new SqlDataAdapter(cmd);
DA1.Fill(DT1);

GridView1.DataSource = DT1;
GridView1.DataBind();
Geeky Ninja
  • 6,002
  • 8
  • 41
  • 54
Vikas
  • 1