Click here to Skip to main content
11,430,109 members (74,445 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Hi everyone ,
Can any one tell me how to convert the List<T> into DataTable in C# ?
Posted 27-Sep-10 0:42am
Edited 27-Sep-10 6:57am
v3
Rate this: bad
good
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.
  Permalink  
v2
Comments
NickMetnik at 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 at 4-Nov-13 0:26am
   
THANK YOU EXCELLENT SOLUTION ,BUT IN MY CASE props GETTING DIFFERENT TABLE AND DIFFERENT VALUES.
WHAT IS THE WRONG.
programmer4programming at 27-Sep-10 7:04am
   
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
programmer4programming at 27-Sep-10 7:04am
   
Thanks alot
Rate this: bad
good
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
  Permalink  
v3
Comments
Hiren Solanki at 27-Sep-10 6:18am
   
Yeah, Thanks arindamrudra.
programmer4programming at 27-Sep-10 7:04am
   
Thanks alot
Rate this: bad
good
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);
}
  Permalink  
v3
Comments
Member 4109163 at 6-Jan-12 1:57am
   
or(int i=0; i<=countries.length; i++) error that .. lengthhave not found..
Roshin Simon at 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 at 20-Dec-13 2:58am
   
how to use this datatable to bind repeater
Member 10353878 at 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)



Advertise | Privacy | Mobile
Web02 | 2.8.150428.2 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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