Gridview 增加多个空行。

时间:2021-02-17 03:04:02
// 自动填充的行数
int numCount = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.DataSource = CreateDataSourceByXianhuiMeng();
GridView1.DataBind();
}
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// 计算自动填充的行数
numCount++;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
// 计算完毕,在此添加缺少的行
int toLeft = TotalRowCount - numCount;
int numCols = GridView1.Rows[0].Cells.Count;

for (int i = 0; i < toLeft; i++)
{
GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
for (int j = 0; j < numCols; j++)
{
TableCell cell = new TableCell();
cell.Text = " ";
row.Cells.Add(cell);
}
GridView1.Controls[0].Controls.AddAt(numCount + 1 + i, row);
}
}
}