Skip to content

Instantly share code, notes, and snippets.

@metinsaylan
Created June 27, 2016 12:00
Show Gist options
  • Save metinsaylan/03a35d2c7dcc3089f98c595bc45f51d2 to your computer and use it in GitHub Desktop.
Save metinsaylan/03a35d2c7dcc3089f98c595bc45f51d2 to your computer and use it in GitHub Desktop.
Convert DataTable to HTML
Public Shared Function ConvertDataTableToHTML(ByVal dt As DataTable, Attributes As String, Optional IncludeHeaders As Boolean = True) As String
Dim TableStart As String = "<table " & Attributes & ">"
Dim TableHeader As String =
<thead>
<tr>
<%=
From col As DataColumn In dt.Columns.Cast(Of DataColumn)()
Select <th><%= col.ColumnName %></th>
%>
</tr>
</thead>.ToString()
Dim TableBody As String =
<tbody>
<%=
From row As DataRow In dt.Rows.Cast(Of DataRow)()
Select
<tr>
<%=
From col As DataColumn In dt.Columns.Cast(Of DataColumn)()
Select <td><%= DataHelper.FormatCellValue(row(col), 3) %></td>
%>
</tr>
%>
</tbody>.ToString()
Dim TableClose As String = "</table>"
Dim Result As String = TableStart
If IncludeHeaders Then
Result = Result & TableHeader
End If
Result = Result & TableBody & TableClose
Return Result
End Function
Public Shared Function FormatCellValue(CellValue As Object, Optional Fraction As Integer = 2) As String
If TypeOf CellValue Is Single Or TypeOf CellValue Is Double Then
Return FormatNumber(CType(CellValue, Single), Fraction)
ElseIf IsNumeric(CellValue) Then
If Not CInt(CellValue) = CSng(CellValue) Then
Return FormatNumber(CType(CellValue, Single), Fraction)
End If
End If
Return CellValue
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment