将空行添加到gridview

时间:2015-11-25 16:17:59

标签: c# asp.net gridview

我想在默认情况下向grdview添加5个空行。我怎么能这样做?

 private void FirstGridViewRow(DateTime theDate)
    {
      DataTable dt = new DataTable();
      DataRow dr = null;
      dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
      dt.Columns.Add(new DataColumn("Col1", typeof(string)));
      dt.Columns.Add(new DataColumn("Col2", typeof(string)));
      dt.Columns.Add(new DataColumn("Col3", typeof(string)));
      dt.Columns.Add(new DataColumn("Col4", typeof(string)));
      dt.Columns.Add(new DataColumn("Col5", typeof(string)));
      dr = dt.NewRow();
      dr["RowNumber"] = 1;
      dr["Col1"] = string.Empty;
      dr["Col2"] = string.Empty;
      dr["Col3"] = string.Empty;
      dr["Col4"] = string.Empty;
      dr["Col5"] = string.Empty;
      dt.Rows.Add(dr);

      ViewState["CurrentTable"] = dt;

      grvStudentDetails.DataSource = dt;
      grvStudentDetails.DataBind();

      for (int i = 1; i < grvStudentDetails.HeaderRow.Cells.Count; i++)
      {
        grvStudentDetails.HeaderRow.Cells[i].Text =
           theDate.AddDays(i - 1).ToString("ddd") + " " + 
           theDate.AddDays(i - 1).ToShortDateString();
      }

      TextBox txn = (TextBox)grvStudentDetails.Rows[0].Cells[1].FindControl("txtName");
      txn.Focus();
      Button btnAdd = (Button)grvStudentDetails.FooterRow.Cells[5].FindControl("ButtonAdd");
      Page.Form.DefaultFocus = btnAdd.ClientID;

    }

1 个答案:

答案 0 :(得分:2)

private void FirstGridViewRow(DateTime theDate)
{
    DataTable dt = new DataTable();
    DataRow dr = null;
    dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
    dt.Columns.Add(new DataColumn("Col1", typeof(string)));
    dt.Columns.Add(new DataColumn("Col2", typeof(string)));
    dt.Columns.Add(new DataColumn("Col3", typeof(string)));
    dt.Columns.Add(new DataColumn("Col4", typeof(string)));
    dt.Columns.Add(new DataColumn("Col5", typeof(string)));
    dr = dt.NewRow();
    for (var i = 1; i < 6; i++)
    {
        dr["RowNumber"] = i;
        dr["Col1"] = string.Empty;
        dr["Col2"] = string.Empty;
        dr["Col3"] = string.Empty;
        dr["Col4"] = string.Empty;
        dr["Col5"] = string.Empty;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
    }
    ViewState["CurrentTable"] = dt;
}
  

您的结果enter image description here