maartenvdv maartenvdv - 8 days ago 7
Vb.net Question

Add row to existing table in webbrowser object

I have loaded a website containing a table-element in my webbrowser object. The id of the table is "MyTable2". I need to be able to add new rows. Every row looks like this:

<tr class="ms-rteTableOddRow-default" style="text-align:center">
<th class="ms-rteTableFirstCol-default"></th>
<td class="ms-rteTableOddCol-default"></td>
<td class="ms-rteTableEvenCol-default">​</td>
<td class="ms-rteTableOddCol-default">​</td>
<td class="ms-rteTableEvenCol-default">​</td>
<td class="ms-rteTableOddCol-default"></td>
<td class="ms-rteTableEvenCol-default">​</td>
</tr>


I am able to get the table-element by

Dim element As HtmlElement = WebBrowser1.Document.GetElementById("table2")


I think it is possible to add it with element.AppendChild(row) but I don't know how to create a htmlelement from my row, and how do I show the new row in my webbrowser?

Answer

I managed to create a solution:

Private Sub AddRowToTable2()
    Dim element As HtmlElement = WebBrowser1.Document.GetElementById("table2")
    Dim mTBody As HtmlElement = element.FirstChild
    Dim mTR As HtmlElement = WebBrowser1.Document.CreateElement("TR")
    Dim mTh As HtmlElement = WebBrowser1.Document.CreateElement("TH")
    Dim mTDOdd1 As HtmlElement = WebBrowser1.Document.CreateElement("TD")
    Dim mTDEven1 As HtmlElement = WebBrowser1.Document.CreateElement("TD")
    Dim mTDOdd2 As HtmlElement = WebBrowser1.Document.CreateElement("TD")
    Dim mTDEven2 As HtmlElement = WebBrowser1.Document.CreateElement("TD")
    Dim mTDOdd3 As HtmlElement = WebBrowser1.Document.CreateElement("TD")
    Dim mTDEven3 As HtmlElement = WebBrowser1.Document.CreateElement("TD")
    mTR.Style = "TEXT-ALIGN: center"
    'controle of het een even of oneven rij moet zijn
    If mTBody.Children.Count Mod 2 = 0 Then
        mTR.SetAttribute("class", "ms-rteTableEvenRow-default")
    Else
        mTR.SetAttribute("class", "ms-rteTableOddRow-default")
    End If
    mTh.SetAttribute("class", "ms-rteTableFirstCol-default")
    mTDEven1.SetAttribute("class", "ms-rteTableEvenCol-default")
    mTDOdd1.SetAttribute("class", "ms-rteTableOddCol-default")
    mTDEven2.SetAttribute("class", "ms-rteTableEvenCol-default")
    mTDOdd2.SetAttribute("class", "ms-rteTableOddCol-default")
    mTDEven3.SetAttribute("class", "ms-rteTableEvenCol-default")
    mTDOdd3.SetAttribute("class", "ms-rteTableOddCol-default")
    'mTh.InnerHtml = <th class="ms-rteTableFirstCol-default">netasq</th>
    'mTDOdd1.InnerHtml = <td class="ms-rteTableOddCol-default">1</td>
    'mTDEven1.InnerHtml = <td class="ms-rteTableEvenCol-default">2</td>
    mTR.AppendChild(mTh)
    mTR.AppendChild(mTDOdd1)
    mTR.AppendChild(mTDEven1)
    mTR.AppendChild(mTDOdd2)
    mTR.AppendChild(mTDEven2)
    mTR.AppendChild(mTDOdd3)
    mTR.AppendChild(mTDEven3)
    mTBody.AppendChild(mTR)
End Sub