I have a DataSet
named DataSet1
. It contains an unknown number of tables and an unknown number of columns and rows in those tables. I would like to loop through each table and look at all of the data in each row for each column. I'm not sure how to code this. Any help would be appreciated!
Add a comment
|
2 Answers
foreach (DataTable table in dataSet.Tables)
{
foreach (DataRow row in table.Rows)
{
foreach (object item in row.ItemArray)
{
// read item
}
}
}
Or, if you need the column info:
foreach (DataTable table in dataSet.Tables)
{
foreach (DataRow row in table.Rows)
{
foreach (DataColumn column in table.Columns)
{
object item = row[column];
// read column and item
}
}
}
Just loop...
foreach(var table in DataSet1.Tables) {
foreach(var col in table.Columns) {
...
}
foreach(var row in table.Rows) {
object[] values = row.ItemArray;
...
}
}
-
I'm confused.. this code will loop through the columns, THEN loop through the rows.. May 30, 2012 at 18:45
-
1I think @Marc was just demonstrating that you can loop through the columns - you don't need to if you are just interested in the row values, but if you iterate the columns you can print out column headers which may be useful.– RobertMSMay 30, 2012 at 18:48
-
3@PhillipSchmidt pretty typical if, say, you want to print the column headers etc May 30, 2012 at 18:49
-
Hello - thanks for the reply - DatSet1.Tables is not valid. ".tables" is not a "member" of DatSet1. The only members are the tables themselves. May 30, 2012 at 19:08
-
3@user902949 I'm assuming
DataSet1
is an instance... not a type. If it is the type, then you need an instance, so justsomeInstance.Tables
. But:.Tables
very much is a member of an instance ofDataSet
: msdn.microsoft.com/en-us/library/… May 30, 2012 at 19:18