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
 
            DynamicTable.AppendLine(HtmlTableStart);
 
            //header row
            CreateTableHeaderRow(DynamicTable);
 
            //table row
            foreach (var product in Products)
            {
                CreateTableRow(product, DynamicTable);
            }
            //Table ends
            DynamicTable.AppendLine(HtmlTableEnd);
 
            return DynamicTable;
        }
 
        //This method create table header row with columns names
        private static void CreateTableHeaderRow(StringBuilder DynamicTable)
        {
 
            DynamicTable.AppendLine(HtmlTrStart);
 
            DynamicTable.AppendLine(HtmlThStart + "Name" + HtmlThEnd);
 
            DynamicTable.AppendLine(HtmlThStart + "Category" + HtmlThEnd);
 
            DynamicTable.AppendLine(HtmlThStart + "Price" + HtmlThEnd);
 
            DynamicTable.AppendLine(HtmlThStart + "Image URL" + HtmlThEnd);
 
            DynamicTable.AppendLine(HtmlTrEnd);
 
        }
 
 
         //This method create new table row with data
        private static void CreateTableRow(Product Product,
 
           StringBuilder emailDynamicTable)
        {
 
            emailDynamicTable.AppendLine(HtmlTrStart);
 
            emailDynamicTable.AppendLine(HtmlTdStart + Product.ProductName + HtmlTdEnd);
 
            emailDynamicTable.AppendLine(HtmlTdStart + Product.CategoryId + HtmlTdEnd);
 
            emailDynamicTable.AppendLine(HtmlTdStart + Product.Price + HtmlTdEnd);
 
            emailDynamicTable.AppendLine(HtmlTdStart + Product.ImageUrl + HtmlTdEnd);
 
            emailDynamicTable.AppendLine(HtmlTrEnd);
 
        }
 
 
    }
 

Retrieve duplicate records from data table using LINQ

 Following code examples explains how to How to find duplicate record using Linq from DataTable.

Find duplicate record for single columns from Data Table using LINQ

     /// <summary>
        /// Returns duplicate records from Employee Table.
        /// </summary>
        private DataTable GetDuplicateEmployeeRecords(DataTable EmployeeData)
        {
            DataTable duplicates = null;
            if (EmployeeData!= null && EmployeeDat.AsEnumerable().Any())
            {
            var duplicatesData = EmployeeData.AsEnumerable().GroupBy(r => r[0]).Where(gr => gr.Count() > 1);
//Check if duplicate records exists
                if (duplicatesData != null &&
                    duplicatesData.Any())
                {
                    duplicates = duplicatesData.CopyToDataTable();
                }
            }
            return duplicates;
        }

Retrieve duplicates for two columns in data table using LINQ

Following linq query LINQ Example to retrieve duplicate records applying grouping on two columns.

     /// <summary>
        /// Returns duplicate records from Employee Table.
        /// </summary>
        private DataTable GetDuplicateEmployeeRecords(DataTable EmployeeData)
        {
            DataTable duplicates = null;
            if (EmployeeData!= null && EmployeeDat.AsEnumerable().Any())
            {
                var duplicatesData = EmployeeData.AsEnumerable().GroupBy(x => new
                {
                    EmployeeId= x.Field<int>("EmployeeId"),
                    CountryId = x.Field<int>("CountryId")
                })
                 .Where(gr => gr.Count() > 1).SelectMany(dupRec => dupRec);
 
                //Check if duplicate records exists
                if (duplicatesData != null &&
                    duplicatesData.Any())
                {
                    duplicates = duplicatesData.CopyToDataTable();
                }
            }
            return duplicates;
        }

Get duplicate records for multiple columns in data table using LINQ

Following linq query LINQ Example to retrieve duplicate records, applying grouping on three columns

     /// <summary>
        /// Returns duplicate records from Employee Table.
        /// </summary>
        private DataTable GetDuplicateEmployeeRecords(DataTable EmployeeData)
        {
            DataTable duplicates = null;
            if (EmployeeData!= null && EmployeeDat.AsEnumerable().Any())
            {
                var duplicatesData = EmployeeData.AsEnumerable().GroupBy(x => new
                {
                    EmployeeId= x.Field<int>("EmployeeId"),
                    CountryId = x.Field<int>("CountryId"),
                    City= x.Field<string>("City")
                })
                 .Where(gr => gr.Count() > 1).SelectMany(dupRec => dupRec);
 
                //Check if duplicate records exists
                if (duplicatesData != null &&
                    duplicatesData.Any())
                {
                    duplicates = duplicatesData.CopyToDataTable();
                }
            }
            return duplicates;
        }