converting datagrid control to html table

I have several datagrid controls in an application i am finishing up on, i need to output the contents to an html file, so i need to convert the datagrid control into a html table, their wouldn't happen to be any easy way of doing this rather then going though cell by cell, extracting the contents and adding it on to a string along with the html code to make it a table is their


Answer this question

converting datagrid control to html table

  • bessermt

    Is this an ASP.NET application The Web Forms DataGrid control already renders its contents to the client as a table.
  • Mark The Archer Evans

    Here's some code I converted from a VB6 COM+ database support component of mine I used with ASP Classic apps. This was one of the first things I did in .NET and, as mentioned above, it would benefit from using StringBuilder rather than classic VB concatenation. I haven't revisited this code since I converted it because I'm using the DataGrid instead in my web apps. Maybe it will give you some ideas if you feel the need to go this route.

    Public Function Query(ByVal sSQL As String, ByVal sTABLEStyle As String, _
    ByVal sTRStyle As String, _
    ByVal sTDStyle As String, _
    ByVal bIncludeColumnHeaders As String, _
    ByVal bIncludeTableTag As Boolean, _
    ByVal sHeaderTRStyle As String, _
    ByVal sHeaderTDStyle As String) As String
    '
    ' Returns data in a HTML Table format for the query
    ' The style strings are inserted in the appropriate spots in the string
    ' The table tag can be omitted to allow direct insertion into a page where
    ' there are other elements before the data
    '
    Dim adpData As SqlDataAdapter
    Dim dsData As DataSet
    Dim colData As DataColumn
    Dim rowData As DataRow
    Dim sRow As String = ""
    Dim sReturn As String = ""
    Dim sColTypes As String()
    Dim nCnt As Integer
    Dim nLoop As Integer

    msLastSQL = sSQL
    If StartConnection() Then
    Try
    adpData = New SqlDataAdapter(sSQL, mconDB)
    dsData = New DataSet
    adpData.Fill(dsData)
    If bIncludeTableTag Then
    sReturn = "<table " & sTABLEStyle & ">"
    End If
    ReDim sColTypes(dsData.Tables(0).Columns.Count - 1)
    If bIncludeColumnHeaders Then
    sReturn = sReturn & "<tr " & sHeaderTRStyle & ">"
    For Each colData In dsData.Tables(0).Columns
    sRow = sRow & "<td " & sHeaderTDStyle & ">" & colData.Caption & "</td>"
    Next
    sReturn = sReturn & sRow & "</tr>"
    End If
    For Each rowData In dsData.Tables(0).Rows
    sRow = "<tr " & sTRStyle & ">"
    nCnt = dsData.Tables(0).Columns.Count - 1
    For nLoop = 0 To nCnt
    sRow = sRow & "<td " & sTDStyle & ">" & rowData.Item(nLoop) & "</td>"
    Next
    sReturn = sReturn & sRow & "</tr>"
    Next
    If bIncludeTableTag Then
    sReturn = sReturn & "</table>"
    End If
    Catch ex As Exception
    msLastError = ex.ToString
    sReturn = ""
    RaiseEvent DBError(msLastError)
    End Try
    Else
    sReturn = ""
    End If
    EndConnection()
    Return sReturn
    End Function



  • Jeff_LIU

            Dim sb As New System.Text.StringBuilder
            sb.Append("<Table border=""0"">" & ControlChars.NewLine)
            For r As Integer = 0 To datOne.RowCount - 2
                sb.Append("<TR>")
                For c As Integer = 0 To datOne.ColumnCount - 1
                    sb.Append("<TD>")
                    sb.Append(datOne.Item(c, r).Value)
                    sb.Append("</TD>")
                Next c
                sb.Append("</TR>" & ControlChars.NewLine)
            Next r
            sb.Append("</Table>" & ControlChars.NewLine)
            Dim sw As New System.IO.StreamWriter("Document.html")
            sw.Write(sb.ToString)
            sw.Close()

  • deji101

    First of all, your idea isn't bad. It's just not as clean. I'd use the Stringbuilder if there is any extensive amount of data because it will be much faster.

    Dim a As Integer = dg1.RowCount -1

    Dim b As Integer = dg1.ColumnCount -1



  • Jonas.S

    well from the sounds of it your idea is harder to impliment, i think i'll stick with my idea, i just need some help doing so.

    Here is my Pseudo Code:

    - Create a string, start off with the <table> tag
    - Go though each cell one-by-one
    - Extract the contents
    - Wrap the contents in the necessary HTML tags
    - Append the result to a string
    - Finish the string with the closing </table> tag

    so the first question in turning this into the actual code is how can i count the row and columns for a for next loop

    here is my actual code:

    Dim r as Integer '# of rows
    Dim c as Integer '# of columns
    For x = 1 to r*c
    Next x

  • Itzik Katzav

    It depends on how you define easy. You could write the data to XML and then do an XML/XSLT transformation to HTML. It's a lot of work but there are a lot of resources on the net to help.

    Writing the XML isn't hard. My only XSLT transformation was done with rtf and it was a bear. HTML is much much simpler because the elements are simpler, more straightforward and is not so much an experience in crytography.



  • converting datagrid control to html table