如何动态地将数据列添加到数据行然后添加到数据表?

时间:2019-06-24 18:10:33

标签: c#

使用C#;我要在数据行中添加列名和列值,然后将数据行动态添加到数据表中。

参考我先前的帖子:How to dynamically add (or) insert values to data column in c#?

问题是,每次我们添加数据列时,它都会添加为新的数据行。 我将必须先构建数据列,然后将其添加到数据行中。

int i = 0;
DataTable dt = new DataTable(); 

// for loop iterating through the cells in the excel -- Starts Here

if ( columnIndex <= iCellCount)
{ 
    dt.Columns.Add(excelRow.Cells[columnIndex].GetText().ToString(), typeof(string));
        dt.Columns[i].ColumnName = excelCell.Placeholder.ColumnName;
        i++;
}

DataRow dtRow;  
dtRow = dt.NewRow();

if (dt.Rows.Count <= excelRows.Count())
{
    dt.Rows.Add(dtRow);
}


// for loop iterating through the cells in the excel -- Ends Here

0 个答案:

没有答案