Loop DataTable

How to loop items in a DataTable

        Dim sbTable As New StringBuilder

        Dim DT As DataTable = DS.Tables("Meta") 'Get your DataTable somehow

        Dim y As Integer = 0

        Dim n As Integer = 0

        sbTable.Append("<table border=""1"">")

        sbTable.Append("<tr>")

        For y = 0 To DT.Columns.Count - 1

            sbTable.Append("<th>" & HttpUtility.HtmlEncode(DT.Columns.Item(y).ColumnName) & "</th>")

        Next

        sbTable.Append("</tr>")

        For n = 0 To DT.Rows.Count - 1

            sbTable.Append("<tr>")

            For y = 0 To DT.Columns.Count - 1

                sbTable.Append("<td>" & HttpUtility.HtmlEncode(DT.Rows(n).Item(y)) & "</td>")

            Next

            sbTable.Append("</tr>")

        Next

        sbTable.Append("</table>")

Post a comment

Comments closed