【问题标题】:asp.net gridview pagination not displayed until refreshingasp.net gridview 分页直到刷新才显示
【发布时间】:2014-11-13 18:13:25
【问题描述】:

我的页面上有DataGridView。当我调用该页面时,整个列表就来了。我决定给GridView添加分页。

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" OnRowDeleting="GridView1_Del" OnSelectedIndexChanging="GridView1_Sel"  OnPageIndexChanging="GridView1_PageIndexChanging">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="Id" SortExpression="id" Visible="false" />
                <asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" />
                <asp:BoundField DataField="author" HeaderText="Author" SortExpression="author" />
                <asp:BoundField DataField="active" HeaderText="Active" SortExpression="active" />
                <asp:CommandField HeaderText="Delete" SelectText="Delete"  ShowDeleteButton="True" ButtonType="Button" />
            </Columns>
        </asp:GridView>



//Page_Load()
gridFill();
GridView1.AllowPaging = true;
GridView1.PageSize = 10;

gridFill() 方法填充GridView

public void gridFill()
{
    conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/inetpub/example.com/db/db.mdb");
    sql = "SELECT  id, name, author, active, FROM [table]";
    dt = new DataTable();

    try
    {
        if (conn.State != ConnectionState.Open) conn.Open();
        comm= new OleDbCommand(sql, conn);
        da= new OleDbDataAdapter(comm);

        da.Fill(dt);

        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }

    catch (System.Data.OleDb.OleDbException ex)
    {
        string msg = "Error: ";
        msg += ex.Message;
        throw new Exception(msg);
    }

    finally
    {
        conn.Close();
    }
}

当我调用页面时,它仍然会一次获取所有行。此外,如果我单击第 11 行或更高版本(因为我将分页限制为 10 行)删除行,我会收到索引错误。

所以我添加了另一个标记为“刷新”的Button,它再次调用了 gridFill() 方法。然后分页生效。

GridView 第一次没有分页可能是什么原因?

【问题讨论】:

    标签: c# asp.net gridview pagination


    【解决方案1】:

    如果我改变行的顺序

    //Page_Load()
    gridFill();
    GridView1.AllowPaging = true;
    GridView1.PageSize = 10;
    

    //Page_Load()
    GridView1.AllowPaging = true;
    GridView1.PageSize = 10;
    gridFill();
    

    它有效。我不敢相信

    【讨论】:

    • 好吧,因为在您的gridFill() 中,DataBind() 方法使用了 GridView 的属性,例如 AllowPagingPageSize。如果您在绑定后设置它们,DataBind() 将使用默认值。
    猜你喜欢
    • 2022-01-21
    • 2019-07-12
    • 2019-11-22
    • 2015-09-07
    • 2016-04-06
    • 1970-01-01
    • 2010-11-06
    • 2021-09-23
    • 2012-02-22
    相关资源
    最近更新 更多