Iterate through DataSet - c#

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!

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;
...
}
}

Related

How to foreach through a list of datatables?

I have a list of 2 DataTables. I want to iterate through each of them, one at a time. How do I do this? There are 0 examples of going through a list of DataTables.
List<DataTable> test = new List<DataTable>();
foreach (DataRow dataRow in TEST.LIST)
{
string value = dataRow.Field<string>("Slave_IO_Running"); //Looks for "Slave_IO_Running" status.
if (value == "Yes")
{
results.Add(siteName + ": WORKING"); //adds working to the visual table
}
else
{
results.Add(siteName + ": REPLICATION ERROR"); //adds not working to the result list
}
break;
}
for each datatable you can use DataTable.Rows and for each row you can access the properties as row["columnName"] or traverse each column in the corresponding row
like
foreach(DataTable table in tables)
{
foreach(DataRow row in table.Rows)
{
foreach(DataColumn column in table.Columns)
{
Console.WriteLine(row[column]);
}
}
}
You can try it like this, assuming both tables contain the same columns:
foreach (DataRow dataRow in test.SelectMany(dt => dt.Rows.OfType<DataRow>()))
{
// your code using the rows
}

adding in SortedDictionary

I want to add table name as string and table value as arraylist if table name exist in same key add record as array list ...here showing key already exist . where i can modify :
SortedDictionary<string, ArrayList> DealInfo = new SortedDictionary<string, ArrayList>();
ArrayList Deal = new ArrayList();
string DealName=string.Empty;
foreach (DataTable table in RecentAddedDeal.Tables)
{
foreach (DataRow dr in table.Rows)
{
if (!DealInfo.ContainsKey(Convert.ToString(dr["DealTab"])))
{
DealName = Convert.ToString(dr["DealTab"]);
}
Deal.Add(dr);
DealInfo.Add(DealName, Deal);
}
}
It's hard to tell exactly what you want since your description says you want to use the TableName as the key, but your code is adding a column from the row as a key.
Going with the idea that the TableName should be the key, something like this should work:
var dealInfo = new SortedDictionary<string, List<DataRow>>();
foreach (DataTable table in RecentAddedDeal.Tables)
{
if (!dealInfo.ContainsKey(table.TableName))
{
dealInfo.Add(table.TableName, table.Rows.Cast<DataRow>().ToList());
}
}
Going with the idea that you want to key off a column in each row for the key, you could do something like:
var dealInfo = new SortedDictionary<string, List<DataRow>>();
foreach (DataTable table in RecentAddedDeal.Tables)
{
foreach (DataRow row in table.Rows)
{
var dealName = row["DealTab"].ToString();
if (dealInfo.ContainsKey(dealName))
{
dealInfo[dealName].Add(row);
}
else
{
dealInfo.Add(dealName, new List<DataRow> {row});
}
}
}
To fill a ListView with data from a row for a specific DealName, you can find the dictionary entry for that deal name and access the list of rows this way:
foreach (DataRow row in dealInfo["SomeDealName"])
{
// Here you have access to the rows where row["DealTab"] == "SomeDealName"
// You can fill a list view with some column value from the row like:
listView1.Add(row["SomeColumnName"].ToString());
}

Get value from dataset cell

I am trying to get the value of a specific cell in a dataset. I have a dataset that has at least two tables(could be more). One of the tables has two columns - Name$ and Value$.
I have to search through the dataset by the name column where the name is "FirstName" and save the value corresponding to that specific name.
Here is what I have so far:
string val = null;
foreach (DataTable dt in ds.Tables)
{
foreach (DataRow dr in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
{
object item = dr[dc];
if (item.ToString().Equals("Name$"))
{
// store the value for that name
}
}
}
}
Any ideas how this could happen, keeping in mind that there could be many tables in the dataset.
EDIT: Here is the full solution:
foreach (DataTable dt in ds.Tables)
{
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
{
if (dc.ColumnName.ToString().Equals("Name$"))
{
if (row["Name$"].ToString().Equals("FirstName"))
{
firstName = (string)row[row.Table.Columns["Name$"].Ordinal + 1];
}
if (row["Name$"].ToString().Equals("LastName$"))
{
lastName = (string)row[row.Table.Columns["Name$"].Ordinal + 1];
}
}
}
}
}
Use ColumnName property
string val = null;
foreach (DataTable dt in ds.Tables)
{
foreach (DataRow dr in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
{
if(dc.ColumnName=="Name")
{
//save
}
}
}
}
foreach (DataRow dr in ds.Tables[0].Rows) //Tables[1]....
{
if(dr["ColumName"].ToString()==....)
// store the value for that name
}
for (int i = 0; i < ds.Tables.Count; i++)
{
foreach (DataRow dr in ds.Tables[i].Rows)
{
//dr....
}
}

C#, Looping through dataset and show each record from a dataset column

In C#, I'm trying to loop through my dataset to show data from each row from a specific column. I want the get each date under the column name "TaskStart" and display it on a report, but its just shows the date from the first row for all rows can anybody help?
foreach (DataTable table in ds.Tables)
{
foreach (DataRow dr in table.Rows)
{
DateTime TaskStart = DateTime.Parse(
ds.Tables[0].Rows[0]["TaskStart"].ToString());
TaskStart.ToString("dd-MMMM-yyyy");
rpt.SetParameterValue("TaskStartDate", TaskStart);
}
}
I believe you intended it more this way:
foreach (DataTable table in ds.Tables)
{
foreach (DataRow dr in table.Rows)
{
DateTime TaskStart = DateTime.Parse(dr["TaskStart"].ToString());
TaskStart.ToString("dd-MMMM-yyyy");
rpt.SetParameterValue("TaskStartDate", TaskStart);
}
}
You always accessed your first row in your dataset.
DateTime TaskStart = DateTime.Parse(dr["TaskStart"].ToString());
foreach (DataRow dr in ds.Tables[0].Rows)
{
//your code here
}
foreach (DataTable table in ds.Tables)
{
foreach (DataRow dr in table.Rows)
{
var ParentId=dr["ParentId"].ToString();
}
}
foreach (DataRow table in ds.Tables[0].Rows)
{
int ForType = Convert.ToInt32(table["Fortype"].ToString());
ds.Tables[0].DefaultView.RowFilter = "ForType ='" + ForType +"'";
rbtnQuestion.DataSource = ds.Tables[0].DefaultView;
rbtnQuestion.DataBind();
}

How to 'foreach' a column in a DataTable using C#?

How do I loop through each column in a datarow using foreach?
DataTable dtTable = new DataTable();
MySQLProcessor.DTTable(mysqlCommand, out dtTable);
foreach (DataRow dtRow in dtTable.Rows)
{
//foreach(DataColumn dc in dtRow)
}
This should work:
DataTable dtTable;
MySQLProcessor.DTTable(mysqlCommand, out dtTable);
// On all tables' rows
foreach (DataRow dtRow in dtTable.Rows)
{
// On all tables' columns
foreach(DataColumn dc in dtTable.Columns)
{
var field1 = dtRow[dc].ToString();
}
}
I believe this is what you want:
DataTable dtTable = new DataTable();
foreach (DataRow dtRow in dtTable.Rows)
{
foreach (DataColumn dc in dtRow.ItemArray)
{
}
}
You can do it like this:
DataTable dt = new DataTable("MyTable");
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn column in dt.Columns)
{
if (row[column] != null) // This will check the null values also (if you want to check).
{
// Do whatever you want.
}
}
}
You can check this out. Use foreach loop over a DataColumn provided with your DataTable.
foreach(DataColumn column in dtTable.Columns)
{
// do here whatever you want to...
}
Something like this:
DataTable dt = new DataTable();
// For each row, print the values of each column.
foreach(DataRow row in dt .Rows)
{
foreach(DataColumn column in dt .Columns)
{
Console.WriteLine(row[column]);
}
}
http://msdn.microsoft.com/en-us/library/system.data.datatable.rows.aspx
In LINQ you could do something like:
foreach (var data in from DataRow row in dataTable.Rows
from DataColumn col in dataTable.Columns
where
row[col] != null
select row[col])
{
// do something with data
}
int countRow = dt.Rows.Count;
int countCol = dt.Columns.Count;
for (int iCol = 0; iCol < countCol; iCol++)
{
DataColumn col = dt.Columns[iCol];
for (int iRow = 0; iRow < countRow; iRow++)
{
object cell = dt.Rows[iRow].ItemArray[iCol];
}
}

Categories