Click here to Skip to main content
14,269,494 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi everyone ,
Can any one tell me how to convert the List<T> into DataTable in C# ?
Posted
Updated 27-Sep-10 5:57am
v3
Rate this:
Please Sign up or sign in to vote.

Solution 1

The Sample code will show you about conversion to Generic List to Datatable.
You will have to use Reflaction for that.
public static DataTable ToDataTable<T>(this IList<T> data)
{
    PropertyDescriptorCollection props =
        TypeDescriptor.GetProperties(typeof(T));
    DataTable table = new DataTable();
    for(int i = 0 ; i < props.Count ; i++)
    {
        PropertyDescriptor prop = props[i];
        table.Columns.Add(prop.Name, prop.PropertyType);
    }
    object[] values = new object[props.Count];
    foreach (T item in data)
    {
        for (int i = 0; i < values.Length; i++)
        {
            values[i] = props[i].GetValue(item);
        }
        table.Rows.Add(values);
    }
    return table;        
}

Please vote and Accept Answer if it Helped.
   
v2
Comments
NickMetnik 11-Oct-13 14:43pm
   
Great solution, saves alot of time. 5 stars.
One situation it will fail is if prop.PropertyType is NULLABLE.

Replace:
table.Columns.Add(prop.Name, prop.PropertyType);

With:
Type t = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
table.Columns.Add(prop.Name, t);
naggg 4-Nov-13 0:26am
   
THANK YOU EXCELLENT SOLUTION ,BUT IN MY CASE props GETTING DIFFERENT TABLE AND DIFFERENT VALUES.
WHAT IS THE WRONG.
hemantwithu 27-Sep-10 7:04am
   
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
hemantwithu 27-Sep-10 7:04am
   
Thanks alot
Rate this:
Please Sign up or sign in to vote.

Solution 2

for Hiren's code we need to use...
using System.Collections.Generic;
using System.ComponentModel;

Thanks Hiren
   
v3
Comments
Hiren solanki 27-Sep-10 6:18am
   
Yeah, Thanks arindamrudra.
hemantwithu 27-Sep-10 7:04am
   
Thanks alot
Rate this:
Please Sign up or sign in to vote.

Solution 3

Hi,
Consider you have a list like
    List<string> countries = new List<string>();
    countries.Add("India");
    countries.Add("Pakistan");
    countries.Add("USA");

    DataTable workTable = new DataTable("Countries");
    DataColumn column;   DataRow row;
    column = new DataColumn();
    column.DataType = System.Type.GetType("System.String");
    column.ColumnName = "CountryName";
    column.AutoIncrement = false;
    column.Caption = "ParentItem";
    column.ReadOnly = false;
    column.Unique = false;
    // Add the column to the table.
    workTable.Columns.Add(column);

for(int i=0; i<=countries.length; i++)
{  
    workTable = table.NewRow();
    workTable["CountryName"] = (String) countries[i];
    workTable.Rows.Add(row);
}
   
v3
Comments
premjit mohanty 6-Jan-12 1:57am
   
or(int i=0; i<=countries.length; i++) error that .. lengthhave not found..
Roshin Simon 12-Oct-13 1:28am
   
for (int i = 0; i <= list.Count; i++)
{
row = workTable.NewRow();
row["CountryName"] = (String)list[i];
workTable.Rows.Add(row);
}
Member 10353878 20-Dec-13 2:58am
   
how to use this datatable to bind repeater
Member 10353878 20-Dec-13 2:58am
   
how to use this datatable to bind repeater

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100