Click here to Skip to main content
15,882,329 members
Articles / Programming Languages / C#

Converting a List to a DataTable

Rate me:
Please Sign up or sign in to vote.
4.98/5 (26 votes)
18 Jul 2011CPOL 95.9K   24   14
I wrote this because I was interested in a Q&A question which asked for exactly that. Since I quite often use DataTables to transfer data, and to display it for debugging I knocked up an extension method to do it.
Then I realized that arrays implement the IList interface... Hmm. So now, I have an Extension Method that converts any List or array based data into a DataTable.
For debugging, that's pretty useful...
So, here it is, for posterity:
public static class ExtensionMethods
    /// <summary>
    /// Converts a List to a datatable
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="data"></param>
    /// <returns></returns>
    public static DataTable ToDataTable<T>(this IList<T> data)
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
        DataTable dt = new DataTable();
        for (int i = 0; i < properties.Count; i++)
            PropertyDescriptor property = properties[i];
            dt.Columns.Add(property.Name, property.PropertyType);
        object[] values = new object[properties.Count];
        foreach (T item in data)
            for (int i = 0; i < values.Length; i++)
                values[i] = properties[i].GetValue(item);
        return dt;

Public NotInheritable Class ExtensionMethods
    ''' <summary>
    ''' Converts a List to a datatable
    ''' </summary>
    ''' <typeparam name="T"></typeparam>
    ''' <param name="data"></param>
    ''' <returns></returns>
    <System.Runtime.CompilerServices.Extension> _
    Public Shared Function ToDataTable(Of T)(data As IList(Of T)) As DataTable
        Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(T))
        Dim dt As New DataTable()
        For i As Integer = 0 To properties.Count - 1
            Dim [property] As PropertyDescriptor = properties(i)
            dt.Columns.Add([property].Name, [property].PropertyType)
        Dim values As Object() = New Object(properties.Count - 1) {}
        For Each item As T In data
            For i As Integer = 0 To values.Length - 1
                values(i) = properties(i).GetValue(item)
        Return dt
    End Function
End Class


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

Written By
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

GeneralMy vote of 5 Pin
Maciej Los25-Dec-14 9:55
mveMaciej Los25-Dec-14 9:55 
GeneralIList => IEnumerable Pin
Andrew Rissing1-Aug-12 5:12
Andrew Rissing1-Aug-12 5:12 
GeneralRe: IList => IEnumerable Pin
OriginalGriff1-Aug-12 5:25
mveOriginalGriff1-Aug-12 5:25 
GeneralReason for my vote of 5 good one. Pin
Nikhil_S28-Feb-12 17:56
professionalNikhil_S28-Feb-12 17:56 
GeneralReason for my vote of 5 coz it works:) Pin
The Doer21-Feb-12 16:53
The Doer21-Feb-12 16:53 
GeneralReason for my vote of 5 Helps in debugging, and one can conv... Pin
Eddy Vluggen7-Nov-11 22:58
professionalEddy Vluggen7-Nov-11 22:58 
GeneralReason for my vote of 4 simple and useful Pin
fesh00419-Jul-11 16:46
fesh00419-Jul-11 16:46 
GeneralReason for my vote of 5 very well done. This is what I was l... Pin
Herman<T>.Instance18-Jul-11 23:05
Herman<T>.Instance18-Jul-11 23:05 
GeneralThe reason is that years agoo in Borland Delphi I created a ... Pin
wvd_vegt18-Jul-11 23:01
professionalwvd_vegt18-Jul-11 23:01 
GeneralReason for my vote of 5 Very usefull (and compact) code. A ... Pin
wvd_vegt18-Jul-11 22:38
professionalwvd_vegt18-Jul-11 22:38 
GeneralRe: That is exactly what it was meant to do... Pin
OriginalGriff18-Jul-11 22:45
mveOriginalGriff18-Jul-11 22:45 
GeneralVery usefull (and compact) code. A pity it copies data into... Pin
wvd_vegt18-Jul-11 22:37
professionalwvd_vegt18-Jul-11 22:37 
GeneralReason for my vote of 5 Thanks for the code. It's very usefu... Pin
Charl Pohlmann18-Jul-11 20:22
Charl Pohlmann18-Jul-11 20:22 
GeneralReason for my vote of 5 very useful. nice idea! Pin
johannesnestler18-Jul-11 3:30
johannesnestler18-Jul-11 3:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.