简体   繁体   English

分页gridview重新绑定每个结果

[英]paging gridview rebinding for each result

i have a gridview with paging featuer,when i want to search item in DB and result displayed in gridview with paging but when i jump another page gridview will Bind and list all items in DB with all pages,how can i solve this problem? 我有一个带有分页功能的gridview,当我想在数据库中搜索项目并在带有分页的gridview中显示结果时,但是当我跳到另一个页面时,gridview将绑定并列出所有页面中的所有项目,我该如何解决这个问题? thanks in your advise 谢谢你的建议

 protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGridView();
    }
}

private void BindGridView()
{
    using (NoavaranModel.NoavaranEntities1 dbContext = new NoavaranModel.NoavaranEntities1())
    {
        var query = from list in dbContext.Students
                    select list;
        lblStudentsCount.Text = query.Count().ToString();
        GridView1.DataSource = query;
        GridView1.DataBind();
    }
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    BindGridView();
    GridView1.DataBind();
}

If I'm understanding correctly, you're calling GridView1.DataBind() at the end of your BindGridView() method, so I don't believe you need to do it again after your call for BindGridView(); 如果我理解正确,那么您将在BindGridView()方法的末尾调用GridView1.DataBind(),因此我认为您在调用BindGridView()之后不需要再次执行此操作; in the PageIndexChanging event: 在PageIndexChanging事件中:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    BindGridView();
}

If this didn't solve it, please let me know what happens in the comments and I'll take another look at it. 如果这不能解决问题,请告诉我评论中发生的情况,我将再次进行研究。

Also, if you're saying that your GridView is filling up with all items from the database (when you're expecting only certain items), that indicates a problem with your query. 另外,如果您说GridView正在填充数据库中的所有项目(当您只希望某些项目时),则表明查询存在问题。 I would try setting a breakpoint and step through the query to see what is coming back, just to make sure you're getting back your expected results. 我会尝试设置一个断点,并逐步执行查询以查看返回的结果,以确保您获得了预期的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM