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

Answers


Data-set data table or DataTable To html table 

In this code example post I will explain how to convert C# data table data into html table Format.

For example, we are retrieving product data in data table format and that data we want to
display in html proper format.

C# Code

Retrieve data in ado.net DataTable from database

var ProductData = ProductDataAccess.GetProductDetails().Tables[0];

 

Converted Data in html String format

 

var ProductDataInHTMLFormat =ConvertDataTableToHTML(ProductData);

Conversion method code

    public static string ConvertDataTableToHTML(DataTable dt)
        {
            
// html Table
            string  html = "<table name='Products'>";
            
//add header row
            html += "<th>";
            
for (int i = 0; i < dt.Columns.Count; i++)
                html += 
"<td>" + dt.Columns[i].ColumnName + "</td>";
            html += 
"</th>";
            
//Add other rows
            foreach (var item in dt.AsEnumerable())
            {
                html += 
"<tr>";
                
//Row data
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                     html += 
"<td>" + item[i].ToString() + "</td>";
                }
                html += 
"</tr>";
            }
            html += 
"</table>";
            
return  html ;
        }

Share your answer


Share as a guest

Please subscribe me to the CodeChef4U.

By creating your new answer, you agree to the privacy policy and terms of service.