Solved

DataTable Traversal.

Posted on 2004-03-29
2
525 Views
Last Modified: 2012-08-14
I have a DataTable with one Column and four Rows.

 How do i iterate through this an load the string values into an array list.

ArrayList myList = new ArrayList();
foreach(DataRow dRow in dt.Rows)
{
  foreach(DataColumn dCol in dRow)
  {
    // What do  i do here. ?

   // or is there any better way to do it, because there is only one column and multiple rows.
  }
 
}


thanks,
steve.
0
Comment
Question by:steve_mick972
  • 2
2 Comments
 
LVL 22

Expert Comment

by:_TAD_
ID: 10708460
ArrayList myList = new ArrayList();
foreach(DataRow dRow in dt.Rows)  
     myList.Add(dRow[0]);
0
 
LVL 22

Accepted Solution

by:
_TAD_ earned 500 total points
ID: 10708543


The curly braces "{" are only required if you want to group more than one line together.  Since we are only executing one line in the foreach block, the curly braces are implicit.


Also... since we only have one column, we don't have to iterate through all of the columns... we will just "hard-code" a zero (the first, and only column).

Finally... the above code more or less copies the object from the table to the arraylist.  If the data you are ALWAYS going to return is of a particular type (like an int) then you may want to change from an arraylist to an int[] (better performance in the long run.



int[] myList = new int[dt.Rows.Count];
int i = 0;
foreach(DataRow dRow in dt.Rows)  
     myList[i++] = (int)dRow[0];

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to use xmlReader and idatareader  ? 4 55
Data is not showing from images 15 37
C# Single Form 8 27
use of unassigned local variable 'id' 6 21
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now