Click here to Skip to main content
13,764,072 members
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 21 Jan 2015
Licenced CPOL

Extension Method for Generic List Collection to DataTable

, 21 Jan 2015
Rate this:
Please Sign up or sign in to vote.
Extension method for Generic Collection to DataTable


This code will help you to get the Extension method for GenericType Collection List for converting them to DataTable.


You must know / may not know about extension method, but using this code class file you can use it without any changes.

Using the Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

namespace coDEalers
    public static class Extension
        public static DataTable ListToDataTable<T>(this IList<T> data, string tableName)
            DataTable table = new DataTable(tableName);

            //special handling for value types and string
            if (typeof(T).IsValueType || typeof(T).Equals(typeof(string)))

                DataColumn dc = new DataColumn("Value");
                foreach (T item in data)
                    DataRow dr = table.NewRow();
                    dr[0] = item;
                PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
                foreach (PropertyDescriptor prop in properties)
                    Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
                foreach (T item in data)
                    DataRow row = table.NewRow();
                    foreach (PropertyDescriptor prop in properties)
                            row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                        catch (Exception ex)
                            row[prop.Name] = DBNull.Value;
            return table;

Points of Interest

This is an easy way for getting GenericList collection to DataTable.


DataTable dt = null;
List<StateList> stateListObj = JsonConvert.DeserializeObject<List<StateList>>(hdn_stateDetails_JSON.Value);
dt = stateListObj.ListToDataTable<StateList>("dtState");


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


About the Author

CEO coDEalers
India India
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionAn Alternative Approach Pin
George Swan21-Jan-15 19:06
memberGeorge Swan21-Jan-15 19:06 
GeneralThoughts Pin
PIEBALDconsult21-Jan-15 18:45
protectorPIEBALDconsult21-Jan-15 18:45 
NewsMy 4 and link to an other article Pin
Jérôme VIBERT21-Jan-15 3:05
memberJérôme VIBERT21-Jan-15 3:05 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.181113.4 | Last Updated 21 Jan 2015
Article Copyright 2015 by MahendraChawhan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid