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 ce
your code is correct. you cn use this code...
Dim tbl As New DataTable()
Dim datarow As DataRow
tbl.Columns.Add("column1")
datarow = tbl.NewRow()
datarow("column1") = "Data1"
tbl.Rows.Add(datarow)
For Each dr As DataRow In tbl.Rows
For Each value In dr.ItemArray
Label1.Text = value.ToString()
Next
Next
You can use DataRow.ItemArray
to do the job also
the code would look something like this
For Each item As var In Row.ItemArray
//do something
Next
For Each row As DataRow In dt.Rows
For Each column As DataColumn in dt.Columns
Console.WriteLine(row(column))
Next column
Next row
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);
}
}
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