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

纵然是瞬间 提交于 2019-12-21 18:39:33

问题


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


回答1:


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



回答2:


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



回答3:


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




回答4:


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


来源:https://stackoverflow.com/questions/15666136/paging-in-gridview-not-working-2nd-page-data-not-showing-data

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