数据库中的记录依赖于分页

本文关键字:依赖于 分页 记录 数据库 | 更新日期: 2023-09-27 18:20:08

protected void GridviewArchived_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridviewArchived.PageIndex = e.NewPageIndex;
    DisplayArchivedNews();
}
private void DisplayArchivedNews()
{
    using (SqlConnection Con = new SqlConnection(connection1))
    {
        SqlCommand Cmd = new SqlCommand("udspGetArchivedNews", Con);
        Cmd.CommandType = CommandType.StoredProcedure;
        Con.Open();
        Cmd.ExecuteNonQuery();
        Con.Close();
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(Cmd);
        da.Fill(ds);
        DataTable dt = ds.Tables[0];
        int totalrecord = ds.Tables[0].Rows.Count;
        GridviewArchived.DataSource = ds;
        GridviewArchived.DataBind();
        if (totalrecord > 0)
        {
            for (int i = 0; i < totalrecord; i++)
            {
                if( File.Exists(Server.MapPath("~//NewsFolder//Page1//" + GridviewArchived.DataKeys[i].Values["News_ID"].ToString().ToString() + (".PDF"))))
                {
                    HyperLink link = new HyperLink();
                    link.Text = "Page1";
                    link.Target = "blank";
                    link.NavigateUrl = "~//NewsFolder//Page1//" + GridviewArchived.DataKeys[i].Values["News_ID"].ToString() + (".pdf");
                    GridviewArchived.Rows[i].Cells[2].Controls.Add(link);
                }
                }
                }

            }
        }

页面大小为 10,但当总记录计数大于页面大小时,它将引发以下异常:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index.

记录自动转到下一页索引的解决方案是什么?

数据库中的记录依赖于分页

要在分页中显示网格视图的记录,有两个选项

一个你可以使用网格视图添加一个对象数据源,并通过它显示分页,另一个选项是使用 jquery Datatable 进行分页。