在C#中遍歷DataTable有以下幾種常用的技巧:
for (int i = 0; i < dataTable.Rows.Count; i++)
{
DataRow row = dataTable.Rows[i];
for (int j = 0; j < dataTable.Columns.Count; j++)
{
Console.WriteLine(row[j]);
}
}
foreach (DataRow row in dataTable.Rows)
{
foreach (DataColumn column in dataTable.Columns)
{
Console.WriteLine(row[column]);
}
}
var query = from DataRow row in dataTable.Rows
select row;
foreach (var row in query)
{
foreach (var item in row.ItemArray)
{
Console.WriteLine(item);
}
}
foreach (DataRow row in dataTable.Rows)
{
foreach (DataColumn column in dataTable.Columns)
{
Console.WriteLine(row[column]);
}
}
以上是一些常用的遍歷DataTable的技巧,可以根據實際需求選擇合適的方法來遍歷DataTable。