Skip to content

Instantly share code, notes, and snippets.

@tahasonmez
Created September 24, 2020 20:15
Show Gist options
  • Save tahasonmez/1f98bd0f16f33b944b2290f53d1f538f to your computer and use it in GitHub Desktop.
Save tahasonmez/1f98bd0f16f33b944b2290f53d1f538f to your computer and use it in GitHub Desktop.
Convert Data Table to HTML Table in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace TSBPCode
{
public static class CollectionUtility
{
public static string CollectionToHTMLTable(DataTable dt, string style)
{
string html = "<table>";
// Add Column Fields
html += "<tr>";
for (int i = 0; i < dt.Columns.Count; i++)
html += "<td>" + dt.Columns[i].ColumnName + "</td>";
html += "</tr>";
// Add Rows
for (int i = 0; i < dt.Rows.Count; i++)
{
html += "<tr>";
for (int j = 0; j < dt.Columns.Count; j++)
html += "<td>" + dt.Rows[i][j].ToString() + "</td>";
html += "</tr>";
}
html += "</table>";
return style + "\n" + html;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment