我正在尝试打印Datatable列名以及值,但是我收到错误当前上下文中不存在行我的代码如下所示。已经尝试了解决方案How do I get column names to print in this C# program?但没有工作
foreach (DataRow row in dtfinal.Rows)
{
foreach (DataColumn col in dtfinal.Columns)
Console.WriteLine(col.ColumnName);
Console.WriteLine(row[col]); //error coming here
}
答案 0 :(得分:0)
这样做
foreach (DataRow row in dtfinal.Rows)
{
foreach (DataColumn column in dtfinal.Columns)
{
Console.Write("Item: ");
Console.Write(column.ColumnName);
Console.Write(" ");
Console.WriteLine(row[column]);
}
}