Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate all items in a given row in the DataTable

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 ,..

like image 758
Adham Avatar asked Mar 07 '13 09:03

Adham


2 Answers

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
like image 87
Arshad Avatar answered Sep 28 '22 03:09

Arshad


For Each row As DataRow In dt.Rows
    For Each column As DataColumn in dt.Columns
        Console.WriteLine(row(column))
    Next column
Next row
like image 44
dis45927 Avatar answered Sep 28 '22 02:09

dis45927