Html helper bynamic table bind for DataTable and generic list model

 Html Helper with DataTable
 
public static MvcHtmlString ToHtmlTable(this HtmlHelper html, DataTable dataTable)
       {
           StringBuilder tempTableSb = new StringBuilder();
           if (dataTable != null && dataTable.Rows.Count > 0)
           {
               tempTableSb.Append("<table class='table table - responsive table - bordered'>");
               tempTableSb.Append("<thead><tr>");
               foreach (DataColumn col in dataTable.Columns)
               {
                   tempTableSb.Append("<th>" + col.Caption + "</th>");
               }
               tempTableSb.Append("</tr></thead><tbody>");
               foreach (DataRow row in dataTable.Rows)
               {
                   tempTableSb.Append("<tr>");
                   foreach (var cell in row.ItemArray)
                   {
                       tempTableSb.Append("<td>" + cell.ToString() + "</td>");
                   }
                   tempTableSb.Append("</tr>");
               }
               tempTableSb.Append("</tbody></table>");
           }
           else
           {
               tempTableSb.Append("<div class='text-danger> Data not found </div>");
           }
 
           return new MvcHtmlString(tempTableSb.ToString());
 
       }
       
 
 Html Helper with Model
 
         public static MvcHtmlString ToHtmlTable<T>(this HtmlHelper html, List<T> data)
       {
           StringBuilder tempTableSb = new StringBuilder();
           if (data != null && data.Count > 0)
           {
               tempTableSb.Append("<table class='table table - responsive table - bordered'>");
               tempTableSb.Append("<thead><tr>");
               PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
               foreach (PropertyDescriptor prop in properties)
                   tempTableSb.Append("<th>" + prop.Name + "</th>");
 
               tempTableSb.Append("</tr></thead><tbody>");
               foreach (T item in data)
               {
                   tempTableSb.Append("<tr>");
                   foreach (PropertyDescriptor prop in properties)
                       tempTableSb.Append("<td>" + prop.GetValue(item) + "</td>");
 
                   tempTableSb.Append("</tr>");
               }
               tempTableSb.Append("</tbody></table>");
           }
           else
           {
               tempTableSb.Append("<div class='text-danger> Data not found </div>");
           }
 
           return new MvcHtmlString(tempTableSb.ToString());
 
       }
 
       public static string ToHtmlTables<T>(List<T> data)

Comments

Popular posts from this blog

What is the importance of EDMX file in Entity Framework

TRIGGER in sql server

Sending Email in asp.net or mvc using gmail or other smpt.