我有一个gridview,我用它来显示数据集结果。问题是我在其中使用分页。但是,当我点击页面#时,它表示我没有处理该事件。我需要重新绑定数据集???
谢谢
请尝试以下代码:
protected void grdView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
FillGrid();
grdView.PageIndex = e.NewPageIndex;
grdView.DataBind();
}
试试吧
在页面加载中
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
loadGrid();
}
}
在pageindexchanging中
private void loadGrid()
{
using (your_bankEntities context = new your_bankEntities()) //use your connection .edmx
{
var jmDados = (from jm in context.yourdbo orderby jm.your fieldkey
select new
{
jm.Field1,
jm.Field2,
jm.Field3,
jm.Field4,
........
jm.n
}).ToList();
GridView1.DataSource = jmDados;
GridView1.DataBind();
}
}
在pageindexchanging中
GridView1.PageIndex = e.NewPageIndex;
loadGrid();
在VB.net中,它与C#没有太大区别,你只需删除每行末尾的分号
Private Sub myGridview_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles myGridview.PageIndexChanging
LoadGridView() //Call your method to load the data into the grid.
myGridview.PageIndex = e.NewPageIndex
myGridview.DataBind()
End Sub
你应该在绑定数据之前设置.PageIndex!否则,您需要额外的点击,这实际上是BindData方法调用的两倍。以下是我测试过的vb代码。
Private Sub GridViewL_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridViewL.PageIndexChanging
GridViewL.PageIndex = e.NewPageIndex
BindData() ' your method to bind data to the grid
End Sub