Solved

DataTable Traversal.

Posted on 2004-03-29
2
561 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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