在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。