How to retrieve all Rows of the DataTable in C#?

Published on May 30, 2013 by abundantcode

Filed under ADO.NET

Last modified May 30, 2013

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 3 (2 votes)

This article have been viewed 4057 times

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();

}

}

}

How to retrieve all Rows of the DataTable in C#?

0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment


x

You might be Interested in these Posts

How to read a connection string from web.config file in ASP.NET ?

Assume that you have a connectionstring configured in the web.config file of your ASP.NET applicatio...

How to Turnoff Frame rate counter in UWP App ?

You can turn off the frame rate counter in your Universal Windows Platform (UAP) apps by following t...

How to Clone an Object in JQuery ?

If you are looking at cloning an object in JQuery , you can do that using the jQuery's extend method...