如何迭代DataTable中给定行中的所有项目 [英] How to iterate all items in a given row in the DataTable
本文介绍了如何迭代DataTable中给定行中的所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何遍历 DataTable
中的给定行中的所有项目
。我有以下代码迭代所有行,我想要另一个For循环迭代给定行中的所有单元格?
How to iterate all items
in a given row in the DataTable
. I have the following code to iterate all rows, I want another For loop to iterate all cells in a given row ?
For Each row As DataRow In dt.Rows
Next row
我可以访问每个行,但是我想访问行上的每一列,因为我不知道列的名称和计数。..
I can access each row, but I want to access each column on the row, as I don't know the name and the count of the columns ,..
推荐答案
你必须循环使用 DataRow.ItemArray 。在 C#
中,我们可以通过以下代码来实现:
You have to loop through DataRow.ItemArray. In C#
, we can do it by following code:
foreach (DataRow dr in dt.Rows)
{
foreach (var item in dr.ItemArray)
{
Console.WriteLine(item);
}
}
这相当于以下VB.NET代码。
This is equivalent to the following VB.NET code.
For Each dr As DataRow In dt.Rows
For Each item In dr.ItemArray
Console.WriteLine(item)
Next
Next
这篇关于如何迭代DataTable中给定行中的所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文