gridview 实现分页后发现数据丢失。(第二页点了之后没数据) 求指导,谢谢了!

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了gridview 实现分页后发现数据丢失。(第二页点了之后没数据) 求指导,谢谢了!相关的知识,希望对你有一定的参考价值。

我的代码:
a.aspx

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="cid" CellPadding="4" ForeColor="#333333" GridLines="None"
AllowPaging="true" PageSize="8"
OnPageIndexChanging="GridView1_PageIndexChanging" Width="449px" >
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField DataField="cid" HeaderText="课程号" ReadOnly="True"
SortExpression="cid" />
<asp:BoundField DataField="cname" HeaderText="课程名" SortExpression="cname" />
<asp:BoundField DataField="credit" HeaderText="学分"
SortExpression="credit" />
<asp:BoundField DataField="hour" HeaderText="课时" SortExpression="hour" />
<asp:BoundField DataField="tid" HeaderText="教师名" SortExpression="" />
<asp:TemplateField><ItemTemplate>
</ItemTemplate></asp:TemplateField>
</Columns>
</asp:GridView>

a.aspx.cs 中:

protected void Page_Load(object sender, EventArgs e)

if (!IsPostBack)

string idk = "95001";
string sqlconn;
sqlconn = @"Server=localhost;Initial Catalog=DBproject;Integrated Security=True;User ID=lin1;Password=1213";
SqlConnection conn = new SqlConnection(sqlconn);
conn.Open();
// string select = "select * from courses where cid not in (select cid from choices where sid='" + idk + "') and cid in (select distinct cid from tcourses where ft in ('Y','y'))";
string select = "select courses.* ,tid from courses,tcourses where courses.cid =tcourses.cid and courses.cid not in (select cid from choices where sid='" + idk + "' )and courses.cid in (select distinct cid from tcourses where ft in ('Y','y'))";
SqlDataAdapter da = new SqlDataAdapter(select , conn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();



protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

GridView1.PageIndex = e.NewPageIndex;
DataBind();//重新绑定一遍数据

参考技术A 这里你不是用的自带的那个DataSource控件,所以这里直接用DataBind()重新绑定时没用的吧!你在GridView1_PageIndexChanging()方法里面应该把他的DataSource重设一次,然后再DataBind绑定一下。 参考技术B 你从数据库取出来的结果 有多少条?会不会只有PageSize="8" 8条追问

有多条。。多于19条。。。。

追答

你上面的代码没问题 看看你的
DataBind();//重新绑定一遍数据
这个函数吧
DataBind();这是系统的函数 你重载它了???
还是你认为用DataBind();实现绑定数据
你GridView1.PageIndex = e.NewPageIndex;
后必须在绑定一遍string select = "select courses.* ,tid from courses,tcourses where courses.cid =tcourses.cid and courses.cid not in (select cid from choices where sid='" + idk + "' )and courses.cid in (select distinct cid from tcourses where ft in ('Y','y'))";
SqlDataAdapter da = new SqlDataAdapter(select , conn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();

本回答被提问者采纳

GridView 排序和分页

【中文标题】GridView 排序和分页【英文标题】:GridView sorting & paging 【发布时间】:2012-08-23 00:06:27 【问题描述】:

我想在我的页面上显示简单的 gridview 并为其提供排序和分页功能。单独排序和分页可以正常工作,但两者的组合却不行。例如,如果我对第一列进行降序排序,然后转到第二页,那么我会看到默认排序(升序)的第二页数据。

我严重依赖这个问题的代码:GridView sorting: SortDirection always Ascending,但问题仍然存在。另外 - 似乎我必须更改我的代码,以便使用 Session 对象而不是 ViewState,还必须弄清楚这一点......

我的代码,经过简化,只有两列:

aspx:

<asp:GridView ID="dgvView" runat="server" AutoGenerateColumns="false" AllowPaging="true"
    PageSize="10" AllowSorting="True" OnPageIndexChanging="DgvViewPageIndexChanging" OnSorting="OnSort">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name">
            <ItemStyle />
        </asp:BoundField>
        <asp:BoundField DataField="BirthDate" HeaderText="Birth date" DataFormatString="0:dd.MM.yyyy"
            SortExpression="BirthDate">
            <ItemStyle />
        </asp:BoundField>
    </Columns>
</asp:GridView>

还有代码隐藏:

public partial class TestPage :Page

    protected void Page_Load(object sender, EventArgs e)
    
        if (!IsPostBack)
        
            DisplayData();
        
    

    private void DisplayData()
    
        Session["TableView"] = GetUsers();
        dgvView.DataSource = Session["TableView"];
        dgvView.DataBind();
    

    protected void DgvViewPageIndexChanging(object sender, GridViewPageEventArgs e)
    
        dgvView.PageIndex = e.NewPageIndex;
        DisplayData();
    

    private List<MyUser> GetUsers()
    
        var users = new List<MyUser>();
        for (int i = 0; i < 100; i++)
        
            users.Add(new MyUser("Name" + i.ToString().PadLeft(2, '0'), new DateTime(2000, 1, 1).AddDays(i)));
        
        return users;
    

    private class MyUser
    
        public string Name  get; private set; 
        public DateTime BirthDate  get; private set; 

        public MyUser(string name, DateTime birthDate)
        
            Name = name;
            BirthDate = birthDate;
        
    

    protected void OnSort(object sender, GridViewSortEventArgs e)
    
        Func<MyUser, object> f;
        if (e.SortExpression == "Name") f = u => u.Name;
        else f = u => u.BirthDate;

        dgvView.DataSource = Sort((List<MyUser>)Session["TableView"], f, GetSortDirection(e.SortExpression));
        dgvView.DataBind();
    

    private List<MyUser> Sort<T>(IEnumerable<MyUser> user, Func<MyUser, T> f, SortDirection sortDirection)
    
        if (sortDirection == SortDirection.Ascending) return user.OrderBy(f).ToList();
        return user.OrderByDescending(f).ToList();
    

    private SortDirection GetSortDirection(string column)
    
        string sessionVariable = "TableSort" + column;
        SortDirection sortDirection;

        if (Session[sessionVariable] == null)
        
            sortDirection = SortDirection.Ascending;
        
        else if ((SortDirection)Session[sessionVariable] == SortDirection.Ascending)
        
            sortDirection = SortDirection.Descending;
        
        else
        
            sortDirection = SortDirection.Ascending;
        

        Session[sessionVariable] = sortDirection;
        return sortDirection;
    

【问题讨论】:

【参考方案1】:

您必须使用 ViewState 才能持久化您的数据

public string SortVariable

     get
     
        if(ViewState["YourKey"] == null)
            return string.Empty;
        return (string)ViewState["YourKey"];
      
     set 
     
         ViewState["YourKey"] = value;
     

当您更改页面时,使用此解决方案,您可以在排序之前在视图状态中获得索引排序。

当您触发 SortCommand 时,您必须设置所描述的视图状态的值

你不需要 Session,因为你在同一个页面,你不需要导航

 protected void OnSort(object sender, GridViewSortEventArgs e)
    

        //Here you set your value SortVariable
        SortVariable = e.SortExpression;
        ...
    

在显示数据中

你创建数据视图和排序处理

private void DisplayData()
    
            //GetSortVariable
            //before   bind with dataview you sort
    

【讨论】:

【参考方案2】:

问题出在

protected void DgvViewPageIndexChanging(object sender, GridViewPageEventArgs e)

    dgvView.PageIndex = e.NewPageIndex;
    DisplayData();

当您调用DisplayData() 方法时,您将您的网格与未排序的全新数据绑定。

Viewstate 中保存你的排序参数(会话对此无用)

如果您使用的是Asp.Net 4,为什么不使用datatables 或jQGrid?

【讨论】:

【参考方案3】:

我在其他两个答案的帮助下解决了这个问题。

我的解决方案:

public partial class TestPage1 : Page

    protected void Page_Load(object sender, EventArgs e)
    
        if (!IsPostBack)
        
            ViewState["Data"] = MyUser.GetUsers();
            ViewState["SortExpression"] = "Name";
            DisplayData("Name", SortDirection.Ascending);
        
    

    private void DisplayData(string sortExpression, SortDirection sortDirection)
    
        Func<MyUser, object> f;
        if (sortExpression == "Name") f = u => u.Name;
        else f = u => u.BirthDate;

        if (sortDirection == SortDirection.Ascending)
        
            dgvView.DataSource = ((IEnumerable<MyUser>)ViewState["Data"]).OrderBy(f).ToList();
        
        else
        
            dgvView.DataSource = ((IEnumerable<MyUser>)ViewState["Data"]).OrderByDescending(f).ToList();
        

        dgvView.DataBind();
    

    protected void DgvViewPageIndexChanging(object sender, GridViewPageEventArgs e)
    
        dgvView.PageIndex = e.NewPageIndex;

        string sortExpression = ViewState["SortExpression"].ToString();
        DisplayData(sortExpression, GetDefaultSortDirection(sortExpression));
    

    protected void OnSort(object sender, GridViewSortEventArgs e)
    
        ViewState["SortExpression"] = e.SortExpression;
        ViewState[e.SortExpression] = GetReverseSortDirection(e.SortExpression);
        DisplayData(e.SortExpression, (SortDirection)ViewState[e.SortExpression]);
    

    private SortDirection GetDefaultSortDirection(string sortExpression)
    
        if (ViewState[sortExpression] == null)
        
            ViewState[sortExpression] = SortDirection.Ascending;
        

        return (SortDirection)ViewState[sortExpression];
    

    private SortDirection GetReverseSortDirection(string sortExpression)
    
        if (ViewState[sortExpression] == null)
        
            ViewState[sortExpression] = SortDirection.Descending;
        
        else
        
            ViewState[sortExpression] = (SortDirection) ViewState[sortExpression] == SortDirection.Descending ? SortDirection.Ascending : SortDirection.Descending;
        

        return (SortDirection)ViewState[sortExpression];
    

【讨论】:

以上是关于gridview 实现分页后发现数据丢失。(第二页点了之后没数据) 求指导,谢谢了!的主要内容,如果未能解决你的问题,请参考以下文章

django 做了搜索之后再做分页 结果显示第一页是正常的 但是按下一页后显示出整个主页的第二页

带有分页和排序的表格内的 Material UI 菜单按钮在第二页后不起作用

在 jquery 数据表中分页后保留文本框值

c# gridview 分页后根据内容改变字体颜色

GridView 排序和分页

java程序,使用poi,用word模板,如何在分页后重复展示表头