Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DataTable Traversal.

Posted on 2004-03-29
2
Medium Priority
?
575 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 2000 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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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!
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

715 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