在数据表的标题行上方添加空行

时间:2019-01-29 04:11:41

标签: c# datatable

我遵循了这个Adding new row to datatable's top来添加空行,但是即使我的索引为0,也不会在数据表的标题上方添加空行。

我尝试过

  

DataRow blankRow = dt.NewRow(); dt.Rows.InsertAt(blankRow,0);

这是我的代码

public void filldatagridview(ExcelWorksheet workSheet)
        {
            DataTable dt = new DataTable();

            //Create the data column
            for (int col = workSheet.Dimension.Start.Column; col <= workSheet.Dimension.End.Column; col++)
            {
                dt.Columns.Add(col.ToString());
            }

           for (int row = 12; row <= 26; row++)
            {
                DataRow newRow = dt.NewRow(); //Create a row
                int i = 0;
                for (int col = workSheet.Dimension.Start.Column; col <= workSheet.Dimension.End.Column; col++)
                {
                    newRow[i++] = workSheet.Cells[row, col].Text;
                }
                dt.Rows.Add(newRow);
            }

            dt.Columns.RemoveAt(0); //remove No 
            dt.Columns.RemoveAt(0); //remove article

            //Get BookCode 
            using (SqlConnection conn = new SqlConnection("Server con.."))
            using (SqlCommand cmd = new SqlCommand(null, conn))
            {
                StringBuilder sb = new StringBuilder("SELECT InvtID AS BOOKCODE FROM InventoryCustomer WHERE Barcode In (");

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (i != 0) sb.Append(",");

                    string name = "@P" + i;
                    cmd.Parameters.AddWithValue(name, dt.Rows[i]["3"]);
                    sb.Append(name);
                }
                sb.Append(")");

                cmd.CommandText = sb.ToString();

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);

                dt.DefaultView.Sort = "BOOKCODE";
                dt = dt.DefaultView.ToTable();

                dt.Columns["BOOKCODE"].SetOrdinal(0);

                dataGridView2.DataSource = dt;
            }
}

 private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
        }

1 个答案:

答案 0 :(得分:0)

您可以尝试一下。由于DataTable为空,因此添加一行会自动将其添加到第0个索引:

DataRow row = dt.NewRow();
dt.Rows.Add(row);

或者您可以这样做:

DataRow blankRow = dt.NewRow(); 

for (int temp = 0; temp < 10; temp++)  // here 10 is number of columns
{
    blankRow[temp] = "";  // use appropriate data type,
}
dt.Rows.InsertAt(blankRow, 0);

更新

在DataBound事件上添加数据源之前:

GridViewRow headerRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);

TableHeaderCell headerCell = new TableHeaderCell();
headerCell.Text = "headercol1”;
headerCell.ColumnSpan = 2;
headerRow.Controls.Add(headerCell);

headerCell = new TableHeaderCell();
headerCell.ColumnSpan = 2;
headerCell.Text = "headercol2”;
headerRow.Controls.Add(headerCell);
dataGridView2.HeaderRow.Parent.Controls.AddAt(0, headerRow); 

这假设您有一个包含4列的gridview。我给headercol1提供了2个colspace,给headercol2提供了2个colspace