HomeSearch

C# DataTable foreach Loop

Use for each on the Rows property from DataTable. Access ItemArray on the rows.

DataTable foreach loop.

DataTable can be used with foreach. It is possible (and often helpful) to loop through all the elements in the DataTable.DataTable

The DataTable class

allows the use of the foreach-loop and its enumerator. But iterating over the items in a DataRow can be confusing.

An example.

We use 2 public instance properties. These are Rows, which returns a typed collection of rows, and ItemArray, which returns a collection of cell values boxed in objects.Object

Note: You can access the DataRows in the first foreach-loop. But the ItemArray property requires that you cast the iteration variable.

ForeachDataRow

GetTable: GetTable creates a DataTable and returns a reference to it. We use 2 foreach-loops to iterate over that data.

Var: Var describes an implicit reference to an object type. These objects are actually of type int, string and DateTime.

Var

Next: This example just displays the object by invoking its ToString method implicitly through the Console.WriteLine method.

Console.WriteLine
C# program that loops over DataTable using System; using System.Data; class Program { static void Main() { DataTable table = GetTable(); foreach (DataRow row in table.Rows) { Console.WriteLine("--- Row ---"); foreach (var item in row.ItemArray) { Console.Write("Item: "); // Print label. Console.WriteLine(item); } } 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)); 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); 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

Notes, cast.

When iterating over the ItemArray elements, it is often easier to cast the cell values to their original types. You can do this with the is-cast or as-cast.Casts

Also: You can also use the generic Field method that receives one type parameter.

DataRow Field

A summary.

We examined foreach, Rows and ItemArray on the DataTable object. We can display and save all cell values in the DataTable at any time.
Home
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, info@dotnetperls.com.