Home > DeveloperSection > Forums > How to convert List to Datatable?
Goti Bandu

Total Post:119

Posted on    October-26-2014 11:26 PM

 ASP.Net C#  ASP.Net 

 1 Reply(s)
 831  View(s)
Rate this:
how to convert list to datatable in asp.net.

Ashish Pandey

Total Post:128

Posted on    October-26-2014 11:27 PM

Hi Goti please visit this example

public static DataTable ToDataTable<T>(List<T> items)
        DataTable dataTable = new DataTable(typeof(T).Name);
    PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
        foreach (PropertyInfo prop in Props)
        foreach (T item in items)
           var values = new object[Props.Length];
           for (int i = 0; i < Props.Length; i++)
                values[i] = Props[i].GetValue(item, null);
      return dataTable;

Modified On Apr-20-2016 01:46:04 AM

Don't want to miss updates? Please click the below button!

Follow MindStick