Question Help populating my listview

aybyd

New member
Joined
Nov 7, 2009
Messages
2
Programming Experience
Beginner
Here is the code I am playing around
I am getting error populating the table cells into my listview. In my codes it is always add only in one cloumn in my lv.

VB.NET:
    Private Sub DumpTables()


        Dim t, c As Integer  ' Used to count tables and cells.

        Dim IWebDocument As HTMLDocument
        Dim IWebElements As IHTMLElementCollection
        Dim ITableElement As HTMLTable
        Dim ICellElement As HTMLTableCell

        ListBox1.Items.Clear()

        'GET DOCUMENT
        IWebDocument = CType(wb.Document, HTMLDocument)

        'GET TABLES
        IWebElements = IWebDocument.getElementsByTagName("TABLE")
        'ListBox1.Items.Add("Length = " & IWebElements.length)

        ' Iterate through all the Tables on the web page.
        t = 0
        For Each ITableElement In IWebElements

            ' Iterate through all the cells within a table.
            c = 0
            For Each ICellElement In ITableElement.cells
ListView1.Items.Add(New ListViewItem(New String() {ICellElement.innerText}))
                c = c + 1
            Next
            t = t + 1
        Next
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        wb.Navigate("http://rushcodes.aybydinnovations.com/coderesult.php")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DumpTables()
    End Sub

Here is my ListView layout
1z99ut.png
 
From the types used I see you use mshtml, this can be done with the standard WebBrowser control and html classes in as of .Net 2.0, so this is used in my sample here. That page only has one table so the sample code skips looking that up and goes straight for the rows (tr). First row is headers (th) so it must be ignored or skipped.
VB.NET:
For Each row As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("tr")
    Dim values As New List(Of String)
    For Each cell As HtmlElement In row.GetElementsByTagName("td")
        values.Add(cell.InnerText)
    Next
    If values.Count > 0 Then
        Me.ListView1.Items.Add(New ListViewItem(values.ToArray))
    End If            
Next
The table headers can also be used to generate the Listview columns:
VB.NET:
For Each header As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("th")
    Me.ListView1.Columns.Add(header.InnerText)
Next
 
Thanks for your reply jonh. It helps me.
I am reading some articles from this dite that i know this cal help me in future.
thanks again..
 
Back
Top