Below is a sample soucrecode demonstrating how to retrieve all rows of the DataTable to a list in C#.
How to retrieve all Rows of the DataTable in C#?
using System; using System.Data; namespace AbundantCode { internal class Program { // Using Foreach to loop through datatable and retreive all rows private static void Main(string[] args) { DataTable dt = new DataTable(); dt.Columns.Add("Name"); dt.Columns.Add("Code"); DataRow row1 = dt.NewRow(); row1["Name"] = "Abundantcode"; row1["Code"] = "1-1-1"; dt.Rows.Add(row1); DataRow row2 = dt.NewRow(); row2["Name"] = "Plenty of sourcecode"; row2["Code"] = "1-1-12"; dt.Rows.Add(row2); foreach (DataRow row in dt.Rows) { string Name = row["Name"].ToString(); Console.WriteLine(Name); } Console.ReadKey(); } } }
Leave a Reply