C# – List<T> To DataTable – DataTable Helper

C# – List<T> To DataTable – DataTable Helper

 
   Column.cs
 
   public class Column : Attribute
   {
     public int Order { get; set; }   // Excel Column Ordering ( Default Vaule is 9999999 )
     public string Name { get; set; }   // DataTable Column Name ( The Vaule is the Field Name. )
     public string DisplayName { get; set; }   // Excel Column Display Name ( Default Vaule is the Actual Field Name. )
     public bool isShow { get; set; }   // The Column would be shown from Excel Column ( Default Vaule is Hidden. )
     public bool isKey { get; set; }   // The Column is a Primary Key Field. It would be shown first from the Excel Column.
     public Type type { get; set; }   // It is Variable Type which is defined for System.Data.DataTable – DataColumn.
   }
 
 
   DataTableHelper.cs
 
   public class DataTableHelper<T> where T : class
   {
     private Dictionary<string, Type> columndict = null;
     private DataTable dt = null;
 
     // Constructor to Create DataTable / Prepare DataColumn only.
     public DataTableHelper()
     {
       this.Initialize();
     }
 
     // Constructor to Create DataTable / Prepare DataColumn / DataRow with Value.
     public DataTableHelper( List<T> list )
     {
       this.Initialize();
       this.SetValue(list);
     }
 
     private void Initialize()
     {
       this.columndict = new Dictionary<string, Type>();
       this.dt = new DataTable();
 
       foreach ( var item in this.GetColumnsList() )
         this.dt.Columns.Add(item);
     }
 
     // Pass List<T> to prepare DataRow with Value on DataTable.
     public void SetValue( List<T> list )
     {
       PropertyInfo pinfo = null;
 
       foreach ( var item in list )
       {
         DataRow dr = this.dt.NewRow();
 
         foreach ( var column in this.columndict )
         {
           if ( column.Value == typeof(Int32) )
           {
             pinfo = item.GetType().GetProperty(column.Key);
             dr[column.Key] = (Int32) pinfo.GetValue(item);
           }
           else if ( column.Value == typeof(string) )
           {
             pinfo = item.GetType().GetProperty(column.Key);
             dr[column.Key] = pinfo.GetValue(item).ToString();
           }
           else if (column.Value == typeof(DateTime))
           {
             pinfo = item.GetType().GetProperty(column.Key);
             dr[column.Key] = (DateTime) pinfo.GetValue(item);
           }
 
           // can define More Column Data Type Casting from here.
 
         }
 
         this.dt.Rows.Add(dr);
       }
     }
 
     // Get Existing DataTable from Object.
     public DataTable GetDataTable()
     {
       return this.dt;
     }
 
     public List<DataColumn> GetColumnsList()
     {
       List<Column> list = new List<Column>();
       List<DataColumn> datacolumnlist = new List<DataColumn>();
       DataColumn datacolumn = null;
 
       PropertyInfo[] props = typeof(T).GetProperties();
 
       foreach ( PropertyInfo prop in props )
       {
         object[] attrs = prop.GetCustomAttributes(true);
 
         foreach ( Column attr in attrs.Where(x => x.GetType() == typeof(Column)) )
         {
           if ( attr != null )
           {
             if ( attr.isShow || attr.isKey )
             {
               if ( attr.isKey )
               {
                 attr.Order = 0;
                 attr.type = prop.PropertyType;
               }
               else
               {
                 if ( attr.Order == 0 )
                   attr.Order = 9999999;
 
                 if ( attr.type == null )
                   attr.type = prop.PropertyType;
               }
 
               attr.Name = prop.Name;
 
               if ( attr.DisplayName == null || attr.DisplayName == "" )
                 attr.DisplayName = prop.Name;
 
               list.Add(attr);
             }
           }
         }
       }
 
       list = list.OrderBy( x => x.Order ).ToList();
 
       this.columndict = new Dictionary<string, Type>();
 
       foreach ( var item in list )
       {
         datacolumn = new DataColumn();
         datacolumn.DataType = item.type;
         datacolumn.Caption = item.DisplayName;
         datacolumn.ColumnName = item.Name;
 
         datacolumnlist.Add(datacolumn);
         columndict.Add(item.Name, item.type);
       }
 
       return datacolumnlist;
     }
   }
 
 
   Model Class with Annotation ( Sample )
 
   public class Person
   {
     [ Column( isKey = true ) ]
     public int Key { get; set; }
     [ Column( Order = 1, isShow = true, DisplayName = "First Name" ) ]
     public string FirstName { get; set; }
     [ Column( Order = 2, isShow = true, DisplayName = "Last Name" ) ]
     public string LastName { get; set; }
     [ Column( Order = 5, isShow = true, DisplayName = "Post Title" ) ]
     public string PostTitle { get; set; }
     [ Column( Order = 4, isShow = true ) ]
     public int Age { get; set; }
     [ Column( isShow = false ) ]
     public string Ref { get; set; }
     [ Column() ]
     public string Dummy { get; set; }
     [ Column( isShow = true ) ]
     public DateTime dt { get; set; }
   }
 

C# – Custom Class Model Annotation ( System.Attribute ) for Prepare DataColumn ( System.Data ) from DataTable Column with Sequence