Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Creating Dynamic HTML table using C#

Creating Dynamic HTML table using C# in Windows Form/Windows Service/Library Project

In this code example post I will explain how to create Dynamic HTML table using C# in Windows Form/Console Application/Windows Service/Windows Library project.

   For example, I want to send products detail email in proper html table format using existing C# generic Product list data. In following code example I will share code only to create dynamic html table and in next code example post I will share html table with email code.

C# Code

   public class DynamicHtmlTable
        //table tag constants
        private const string HtmlTableStart = "<table cellspacing=0 cellpadding=0 style=\"border-collapse:collapse; text-align:center;\">";
        private const string HtmlTableEnd = "</table>";
        private const string HtmlTrStart = "<tr>";
        private const string HtmlTrEnd = "</tr>";
        private const string HtmlThStart = "<th style=\" border-color:#5c87b2; border-style:solid;text-align:center;border-width:thin;\">";
        private const string HtmlThEnd = "</th>";
        private const string HtmlTdStart = "<td style=\" border-color:#5c87b2; border-style:solid;border-width:thin;\">";
        private const string HtmlTdEnd = "</td>";
        public void GetTableData()
            var Products=Dal.DAL.GetProducts();
            var EmailData = BuildDynamicTable(Products, "Dynmaic Html Table").ToString();
        //This method creates dynamic table
        private StringBuilder BuildDynamicTable(List<Product> Products,string appName)
            var DynamicTable = new StringBuilder();
            DynamicTable.AppendFormat("<h2>CodeChef4u Example To Create{0}</h2>", appName);
            //dynamic table
            //header row
            //table row
            foreach (var product in Products)
                CreateTableRow(product, DynamicTable);
            //Table ends
            return DynamicTable;
        //This method create table header row with columns names
        private static void CreateTableHeaderRow(StringBuilder DynamicTable)
            DynamicTable.AppendLine(HtmlThStart + "Name" + HtmlThEnd);
            DynamicTable.AppendLine(HtmlThStart + "Category" + HtmlThEnd);
            DynamicTable.AppendLine(HtmlThStart + "Price" + HtmlThEnd);
            DynamicTable.AppendLine(HtmlThStart + "Image URL" + HtmlThEnd);
         //This method create new table row with data
        private static void CreateTableRow(Product Product,
           StringBuilder emailDynamicTable)
            emailDynamicTable.AppendLine(HtmlTdStart + Product.ProductName + HtmlTdEnd);
            emailDynamicTable.AppendLine(HtmlTdStart + Product.CategoryId + HtmlTdEnd);
            emailDynamicTable.AppendLine(HtmlTdStart + Product.Price + HtmlTdEnd);
            emailDynamicTable.AppendLine(HtmlTdStart + Product.ImageUrl + HtmlTdEnd);