【问题标题】:How to Set Last Row Data Paging如何设置最后一行数据分页
【发布时间】:2019-11-08 05:03:29
【问题描述】:

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

I set Page size: 10 in gridview

这是我的后台代码:

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();  
    }

结果:

The page 7 have 10 rows

but after I change page 7 to last page have only less than 10 rows.

I want the last page have 10 rows although data table 7 rows

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

【问题讨论】:

  • 您可以使用 Query 来检查表格子元素,只要它的行数少于该行,您就可以向其追加额外的行......如果你愿意......

标签: c# sql asp.net data-paging


【解决方案1】:

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

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();  
}

【讨论】:

  • @Roberts 很高兴为您提供帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多