Solved

DataTable Traversal.

Posted on 2004-03-29
2
511 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_
Comment Utility
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
Comment Utility


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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

16 Experts available now in Live!

Get 1:1 Help Now