Convert a DataTable to a List<> using C# or VB.NET

A user recently asked me a question on converting a DataTable to a List<> in .NET 2.0. Here’s the code to do so:


// Assuming there is a DataTable called dt
List<DataRow> drlist = new List<DataRow>();

foreach (DataRow row in dt.Rows)


' Assuming there is a DataTable called dt
Dim drlist As New List(Of DataRow)()

For Each row As DataRow In dt.Rows
drlist.Add(CType(row, DataRow))
Next row

Please note that this is just a prototype and may not cover all scenarios.

Note: I have not tested this but in .NET 3.5, you should be able to do this:

List<DataRow> drlist = dt.AsEnumerable().ToList();

Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal


3 Responses to "Convert a DataTable to a List<> using C# or VB.NET"
  1. Anonymous said...
    January 11, 2010 at 10:11 AM

    thanks for the post it was very useful

    protected void RetrieveData_Click(object sender, EventArgs e)
    List drList = new List();
    foreach (DataRow row in dt.Rows)
    foreach (DataRow s in drList)
    Response.Write(s.ItemArray[0].ToString()+" "+s.ItemArray[1].ToString()+"

  2. Anonymous said...
    September 1, 2010 at 6:19 AM

    List list = new List(dataTable.Select());
    List customList = list.ConvertAll(e=>new CustomType{ Member1= (e["Processed"])

  3. Sesha Giri said...
    January 9, 2013 at 2:29 AM

    hi bro when in convert datatable to list i am getting error plz help me out!!!!
    public string RecieveClassRoom(List classes)
    //List ClassListed = new List();
    List drList = new List().ToList();

    using (SqlConnection conn = new SqlConnection(conStr))
    SqlDataAdapter ada = new SqlDataAdapter ( "sp_FetchCategories" , conn);
    DataTable dt = new DataTable ();

    foreach(DataRow row in dt.Rows)
    drList.Add(new ClassRoom{RollNum='1'});
    return string.Format("Thank you, {0} number of rows recieved!",rowsInserted);


Copyright © 2009-2016 All Rights Reserved for by Suprotim Agarwal | Terms and Conditions