How to convert List to DataTable

very recently I have published a new article based on "How to convert DataTable to generic List"

Now I have discussed about the vice verse -"convert List to DataTable"


Here we use PropertyDescriptor ComponentModel class.

public static DataTable ToDataTable<T>(this IList<T> data)
PropertyDescriptorCollection properties =
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
(prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
? 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;
return table;


Popular posts from this blog

The model backing the 'MyDBContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.

"This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet."

Check/Uncheck all items in a CheckBoxList using Javascript