Gridview 中的分页不起作用第二页数据未显示数据?

问题描述 投票:0回答:4
asp.net gridview pagination
4个回答
13
投票

像下面这样使用

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

0
投票

您的数据表在每个页面加载时都会初始化,因此您需要获取数据表 每次将其绑定到网格之前。 在将页面绑定到网格之前设置页面索引

所以你的代码应该是这样的

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

0
投票

无需在 exportGrdVw_PageIndexChanging 上调用数据库。只需将 DataTable 表声明为 static


-1
投票

你可以试试这个:

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();
© www.soinside.com 2019 - 2024. All rights reserved.