0

我无法为分页设置最后一行。

我在gridview中设置页面大小:10

这是我的背后代码:

protected void Page_Load(object sender, EventArgs e)
    {
        BindData();
    }

    public void BindData()
    {
        string strConnection = @"Data Source=.\sa;Initial Catalog=Northwind;Integrated Security=SSPI;";

        SqlConnection con = new SqlConnection(strConnection);
        con.Open();
        SqlCommand cmd = new SqlCommand("select ProductId, ProductName, SupplierId from Products", con);
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();  

    }

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

结果:

第 7 页有 10 行

但是在我将第 7 页更改为最后一页之后,只有不到 10 行。

我希望最后一页有 10 行,虽然数据表有 7 行

任何人都可以改进我的代码。

4

1 回答 1

0

您需要在 Dataset.table 中添加空行(添加的行数取决于小于页面大小的行数)

protected void Page_Load(object sender, EventArgs e)
    {
        BindData();
    }

public void BindData()
{
    string strConnection = @"Data Source=.\sa;Initial Catalog=Northwind;Integrated Security=SSPI;";

    SqlConnection con = new SqlConnection(strConnection);
    con.Open();
    SqlCommand cmd = new SqlCommand("select ProductId, ProductName, SupplierId from Products", con);
    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(ds);
    int rowcount = ds.Tables[0].Rows.Count;
    int remainingCount = 10 - (rowcount % 10);
    for (int i = 0; i < remainingCount; i++)
    {
        DataRow row = ds.Tables[0].NewRow();
        ds.Tables[0].Rows.Add(row);
    }
    GridView1.DataSource = ds;
    GridView1.DataBind();
    con.Close();  

}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    BindData();  
}
于 2019-06-26T11:32:46.673 回答