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

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

 
   The Following Code can be used for preparing DataTable Object from the NPOI Excel Export Development.
 
   If the Class Model is from Entity Framework ( Database Domain Model ).
   It is recommend to define a View Model with the Following Annotation instead of assigning this from Database Domain Model.

 
 
   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>
   {
 
     … …
 
     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();
 
       foreach( var item in list )
       {
         datacolumn = new DataColumn();
         datacolumn.DataType = item.type;
         datacolumn.Caption = item.DisplayName;
         datacolumn.ColumnName = item.Name;
 
         datacolumnlist.Add(datacolumn);
       }
 
       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; }
   }