Skip to content
Loading
  • Ananth G
    +1
    1. DataTable dt =   YourList.ToDataTable();  
    2.   
    3. public static DataTable ToDataTable(this List iList)  
    4.         {  
    5.             DataTable dataTable = new DataTable();  
    6.             PropertyDescriptorCollection propertyDescriptorCollection =  
    7.                 TypeDescriptor.GetProperties(typeof(T));  
    8.             for (int i = 0; i < propertyDescriptorCollection.Count; i++)  
    9.             {  
    10.                 PropertyDescriptor propertyDescriptor = propertyDescriptorCollection[i];  
    11.                 Type type = propertyDescriptor.PropertyType;  
    12.   
    13.                 if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))  
    14.                     type = Nullable.GetUnderlyingType(type);  
    15.   
    16.   
    17.                 dataTable.Columns.Add(propertyDescriptor.Name, type);  
    18.             }  
    19.             object[] values = new object[propertyDescriptorCollection.Count];  
    20.             foreach (T iListItem in iList)  
    21.             {  
    22.                 for (int i = 0; i < values.Length; i++)  
    23.                 {  
    24.                     values[i] = propertyDescriptorCollection[i].GetValue(iListItem);  
    25.                 }  
    26.                 dataTable.Rows.Add(values);  
    27.             }  
    28.             return dataTable;  
    29.         }  
    Ur list 
     
    1. List licenselist = new List();  
    2. DataTable dt =   licenselist.ToDataTable();  
     
    +1
  • Jasbeer Singh
    +1
     You ca use this method to get Datatable from list.
    You can pass list to this function
     
     
    +1
  • Ananth G
    +1
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
     
     
    add above reference
    +1
  • 0
    Ananth,
    PropertyDescriptorCollection shows an error-Missing assembly reference 
    0
  • 0
    Ananth,
    There is an error populating in my code . 
    Error 5 Extension method must be defined in a non-generic static class 
    0
  • Midhun Tp
    0
    Hi,
     
    Please go through below article -
     
    http://www.c-sharpcorner.com/UploadFile/1a81c5/list-to-datatable-converter-using-C-Sharp/ 
    0
  • Khaja Moizuddin
    0
    ust add this function and call it, it will convert List to DataTable. public static DataTable ToDataTable(List items) { DataTable dataTable = new DataTable(typeof(T).Name); //Get all the properties PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in Props) { //Setting column names as Property names dataTable.Columns.Add(prop.Name); } foreach (T item in items) { var values = new object[Props.Length]; for (int i = 0; i < Props.Length; i++) { //inserting property values to datatable rows values[i] = Props[i].GetValue(item, null); } dataTable.Rows.Add(values); } //put a breakpoint here and check datatable return dataTable; }
    0
  • Harshal Patil
    0

    public DataTable ToDataTable(List items)

    {

    DataTable dataTable = new DataTable(typeof(T).Name);

    //Get all the properties

    PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

    foreach (PropertyInfo prop in Props)

    {

    //Setting column names as Property names

    dataTable.Columns.Add(prop.Name);

    }

    foreach (T item in items)

    {

    var values = new object[Props.Length];

    for (int i = 0; i < Props.Length; i++)

    {

    //inserting property values to datatable rows

    values[i] = Props[i].GetValue(item, null);

    }

    dataTable.Rows.Add(values);

    }

    //put a breakpoint here and check datatable

    return dataTable;

    }

    0