Paging in Gridview not working 2nd page data not showing data?

亡梦爱人 提交于 2019-12-04 10:42:09

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();
}
Nishant

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();
}

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

Vikas

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!