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.
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]