Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

The Sample code will show you about conversion to Generic List to Datatable.
You will have to use Reflaction for that.
C#
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.
 
Share this answer
 
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
for Hiren's code we need to use...
using System.Collections.Generic;
using System.ComponentModel;

Thanks Hiren
 
Share this answer
 
v3
Comments
Hiren solanki 27-Sep-10 6:18am    
Yeah, Thanks arindamrudra.
hemantwithu 27-Sep-10 7:04am    
Thanks alot
Hi,
Consider you have a list like
C#
    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);
}
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900