
DataTable can be used with foreach. It is possible to loop through all the elements in the DataTable and display them to the screen or record them for debugging. The DataTable allows the use of the foreach-loop and its enumerator. But iterating over the items in a DataRow can be confusing.
This C# example code uses foreach on the Rows property from DataTable.

First the DataTable class in the System.Data namespace from the .NET Framework provides two public instance properties that are ideal for this task: the Rows property, which returns a typed collection of rows, and the ItemArray property, which returns a collection of cell values boxed in objects.
You can access the DataRow elements easily in the first foreach-loop, but the ItemArray property requires that you either cast the iteration variable or just display it.
Looping over DataTable instance [C#]
using System;
using System.Data;
class Program
{
static void Main()
{
DataTable table = GetTable(); // Get the data table.
foreach (DataRow row in table.Rows) // Loop over the rows.
{
Console.WriteLine("--- Row ---"); // Print separator.
foreach (var item in row.ItemArray) // Loop over the items.
{
Console.Write("Item: "); // Print label.
Console.WriteLine(item); // Invokes ToString abstract method.
}
}
Console.Read(); // Pause.
}
/// <summary>
/// Generates DataTable filled with patient information.
/// </summary>
static DataTable GetTable()
{
DataTable table = new DataTable(); // New data table.
table.Columns.Add("Dosage", typeof(int)); // Add five columns.
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
table.Rows.Add(15, "Abilify", "Jacob", DateTime.Now); // Add five data rows.
table.Rows.Add(40, "Accupril", "Emma", DateTime.Now);
table.Rows.Add(40, "Accutane", "Michael", DateTime.Now);
table.Rows.Add(20, "Aciphex", "Ethan", DateTime.Now);
table.Rows.Add(45, "Actos", "Emily", DateTime.Now);
return table; // Return reference.
}
}
Output
--- Row ---
Item: 15
Item: Abilify
Item: Jacob
Item: 7/10/2009 8:31:40 AM
--- Row ---
Item: 40
Item: Accupril
Item: Emma
Item: 7/10/2009 8:31:40 AM
--- Row ---
Item: 40
Item: Accutane
Item: Michael
Item: 7/10/2009 8:31:40 AM
--- Row ---
Item: 20
Item: Aciphex
Item: Ethan
Item: 7/10/2009 8:31:40 AM
--- Row ---
Item: 45
Item: Actos
Item: Emily
Item: 7/10/2009 8:31:40 AM
Description. This program first generates an example DataTable instance that contains information about prescription medications and patient names, as for a physician's office. The GetTable method locally allocates the data and then returns a reference to that data on the managed heap. The Main entry point them uses two foreach-loops to iterate over that data.
Foreach Loop ExamplesConsole.Write call on objects. In the innermost foreach-loop body in the Main method, the "var item" declaration describes an implicit reference to an object type. These objects are actually of type int, string, and DateTime, and you can cast them with the 'is' or 'as' operators if required. This example just displays the object by invoking its ToString method implicitly through the Console.WriteLine method.
Console.WriteLine
Casting iteration variables. When iterating over the ItemArray elements, it is often easier to actually cast the cell values to their original object types. You can do this with the 'is' or 'as' operators or can use the generic Field method that receives one type parameter. You can find examples of this casting in the article on DataRow.
DataRow Examples Is Cast Example As Cast Example
You can find resources on the DataTable and using its methods and data references contained in its internals. The example in this article focuses entirely on the foreach-loop construct, but often you will want to interface with SQL Server or other tools and controls such as DataGridView using DataTable. The DataTable article is a good starting point.
DataTable Examples
We examined the foreach-loop construct on the DataTable object using the C# programming language. The code example in this article allows you to display and save all cell values in the DataTable at any time. We noted issues related to the foreach-statements and how you can invoke the instance Rows and ItemArray properties for this purpose.
Data