Home
C#
DataTable foreach Loop
Updated Feb 19, 2023
Dot Net Perls
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
Looping notes. 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.
foreach
DataRow
Detail GetTable creates a DataTable and returns a reference to it. We use 2 foreach-loops to iterate over that data.
Detail When iterating over the ItemArray elements, it is often easier to cast the cell values to their original types.
is
as
using System; using System.Data; class Program { static void Main() { DataTable table = GetTable(); foreach (DataRow row in table.Rows) { foreach (var item in row.ItemArray) { Console.WriteLine(item); } } } static DataTable GetTable() { DataTable table = new DataTable(); table.Columns.Add("Size", typeof(int)); table.Columns.Add("Category", typeof(string)); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Date", typeof(DateTime)); // Use 1 row for simplicity. table.Rows.Add(15, "dog", "Spot", DateTime.Now); return table; } }
Item: 15 Item: dog Item: Spot Item: 11/3/2020 5:33:27 AM
Field foreach. You can also use the generic Field method that receives one type parameter. We can loop over Rows with foreach, and then use Field to get each cell.
DataRow Field
Detail Var describes an implicit reference to an object type. These objects are actually of type int, string and DateTime.
var
using System; using System.Data; class Program { static void Main() { DataTable table = GetTable(); foreach (DataRow row in table.Rows) { var field1 = row.Field<int>(0); var field2 = row.Field<string>(1); var field3 = row.Field<string>(2); var field4 = row.Field<DateTime>(3); Console.WriteLine("{0} {1} {2} [{3}]", field1, field2, field3, field4); } } static DataTable GetTable() { DataTable table = new DataTable(); table.Columns.Add("Size", typeof(int)); table.Columns.Add("Category", typeof(string)); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Date", typeof(DateTime)); table.Rows.Add(15, "dog", "Spot", DateTime.Now); return table; } }
15 dog Spot [11/3/2020 5:38:29 AM]
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.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
No updates found for this page.
Home
Changes
© 2007-2025 Sam Allen