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