Anoop Bansal

Anoop Bansal

  • NA
  • 58
  • 6.2k

List of Array to datatable

Apr 22 2017 4:34 PM
Hi,
 
I have a class
  1. public class AppUpdator  
  2.    {  
  3.        public string ApplicationName { getset; }  
  4.   
  5.        public Version CurrentVersion { getset; }  
  6.   
  7.        public Version InstalledVersion { getset; }  
  8.   
  9.        public DateTime ReleaseDate { getset; }  
  10.   
  11.        public StringBuilder Description { getset; }  
  12.   
  13.        public List<FileNames> filenames = new List<FileNames>();  
  14.   
  15.    }  
  16.    public struct FileNames  
  17.    {  
  18.        public string FileName { getset; }  
  19.   
  20.        public string DestinationFolder { getset; }  
  21.   
  22.        public byte FileSize { getset; }  
  23.   
  24.        public enum FileOperation  
  25.        {  
  26.            Copy,  
  27.            Unzip,  
  28.            Delete,  
  29.            Execute  
  30.        }  
  31.   
  32.        public FileOperation Operation { getset; }  
  33.   
  34.        public string DownloadURL { getset; }  
  35.    }  
I use Generic to convert list to Datatable
  1. public DataTable CreateDataTable<T>(IEnumerable<T> list)  
  2.         {  
  3.             Type type = typeof(T);  
  4.             var properties = type.GetProperties();  
  5.   
  6.             DataTable dataTable = new DataTable();  
  7.             foreach (PropertyInfo info in properties)  
  8.             {  
  9.                 dataTable.Columns.Add(new DataColumn(info.Name, Nullable.GetUnderlyingType(info.PropertyType) ?? info.PropertyType));  
  10.             }  
  11.   
  12.             foreach (T entity in list)  
  13.             {  
  14.                 object[] values = new object[properties.Length];  
  15.                 for (int i = 0; i < properties.Length; i++)  
  16.                 {  
  17.                     values[i] = properties[i].GetValue(entity);  
  18.                 }  
  19.   
  20.                 dataTable.Rows.Add(values);  
  21.             }  
  22.   
  23.             return dataTable;  
  24.         }  
But datatable doesnot return the list of files (as highlighted), it excludes the List<Filenames> variable and return only 5 columns to datatable.
 
How to include that 

Answers (3)