Created
September 24, 2020 20:15
-
-
Save tahasonmez/1f98bd0f16f33b944b2290f53d1f538f to your computer and use it in GitHub Desktop.
Convert Data Table to HTML Table in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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