Convert from List to Datatable

Post in here as my backup :

public static DataTable ConvertToDatatable<T>(List<T> data)
PropertyDescriptorCollection props =
DataTable table = new DataTable();
for (int i = 0; i < props.Count; i++)
PropertyDescriptor prop = props[i];
if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
table.Columns.Add(prop.Name, prop.PropertyType.GetGenericArguments()[0]);
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);
return table;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.