Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
974 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...