locked
get column name in ListView RRS feed

  • Question

  • How to write Query to Display Column Names of HTML table in ListView 
    <HTML DIR=LTR>
    <HEAD>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=Windows-1252">
    <TITLE>DailyReport</TITLE>
    </HEAD>
    <BODY>
    <TABLE DIR=LTR BORDER>
    <CAPTION>DailyReport</CAPTION>
    <TR>
    <TH>SLNo</TH>
    <TH>Day</TH>
    <TH>Date</TH>
    <TH>Task</TH>
    <TH>TaskPurpose</TH>
    <TH>TimeTakeninMinutes</TH>
    <TH>TaskDeliverable</TH>
    <TH>Purpose</TH>
    </TR>
    </TABLE>
    </BODY>
    </HTML>
    as of now i am displaying (Select * from HTML_Table) in Datagridview
    but don't know how to get header name (<tr>)
    Friday, July 15, 2011 7:16 AM

Answers

  • Hi Amit,

    You cannot access a webpage direct in this way.

    Use for that the Agility pack or use MSHTML

    Agility pack

    http://htmlagilitypack.codeplex.com/

    MSHTML sample on our website

    http://www.vb-tips.com/dbpages.aspx?Search=mshtml

     


    Success
    Cor
    • Proposed as answer by Mike Feng Wednesday, July 20, 2011 3:09 AM
    • Marked as answer by Mike Feng Wednesday, July 27, 2011 9:30 AM
    Saturday, July 16, 2011 9:27 AM
  • Hi Amit_Kumar,

    Here is a related thread about read the HTML table to datagridview: http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/e1bb6e15-b2d9-4aa1-aa51-4a303a752b34

    In that thread, I posted the code read the HTML tables to dataset, and this code can read the HTML table to listView:

        Public Function ConvertHTMLTablesToListView(ByVal HTML As String) As ListView
          ' Declarations 
          Dim lv As New ListView
          Dim lvi As ListViewItem
          Dim TableExpression As String = "<table[^>]*>(.*?)</table>"
          Dim HeaderExpression As String = "<th[^>]*>(.*?)</th>"
          Dim RowExpression As String = "<tr[^>]*>(.*?)</tr>"
          Dim ColumnExpression As String = "<td[^>]*>(.*?)</td>"
          Dim HeadersExist As Boolean = False
          Dim iCurrentColumn As Integer = 0
          Dim iCurrentRow As Integer = 0
          ' Get a match for all the tables in the HTML 
          Dim Tables As MatchCollection = Regex.Matches(HTML, TableExpression, RegexOptions.Multiline Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)
          ' Loop through each table element 
          For Each Table As Match In Tables
            ' Reset the current row counter and the header flag 
            iCurrentRow = 0
            HeadersExist = False
            ' Create the relevant amount of columns for this table (use the headers if they exist, otherwise use default names) 
            If Table.Value.Contains("<th") Then
              ' Set the HeadersExist flag 
              HeadersExist = True
              ' Get a match for all the rows in the table 
              Dim Headers As MatchCollection = Regex.Matches(Table.Value, HeaderExpression, RegexOptions.Multiline Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)
              ' Loop through each header element 
              For Each Header As Match In Headers
                lv.Columns.Add(Header.Groups(1).ToString)
              Next
            Else
              For iColumns As Integer = 1 To Regex.Matches(Regex.Matches(Regex.Matches(Table.Value, TableExpression, RegexOptions.Multiline Or RegexOptions.Singleline Or RegexOptions.IgnoreCase).Item(0).ToString, RowExpression, RegexOptions.Multiline Or RegexOptions.Singleline Or RegexOptions.IgnoreCase).Item(0).ToString, ColumnExpression, RegexOptions.Multiline Or RegexOptions.Singleline Or RegexOptions.IgnoreCase).Count
                lv.Columns.Add("Column " & iColumns)
              Next
            End If
            ' Get a match for all the rows in the listview 
            Dim Rows As MatchCollection = Regex.Matches(Table.Value, RowExpression, RegexOptions.Multiline Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)
            ' Loop through each row element 
            For Each Row As Match In Rows
              ' Only loop through the row if it isn't a header row 
              If Not (iCurrentRow = 0 And HeadersExist = True) Then
                ' Create a new row and reset the current column counter 
                lvi = New ListViewItem
                ' Get a match for all the columns in the row 
                Dim Columns As MatchCollection = Regex.Matches(Row.Value, ColumnExpression, RegexOptions.Multiline Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)
                ' Loop through each column element 
                For Each Column As Match In Columns
                  ' Add the value to the ListViewItem 
                  lvi.SubItems.Add(Column.Groups(1).ToString)
                  ' Increase the current column  
                  iCurrentColumn += 1
                Next
                ' Add the ListViewItem to the ListView
                lv.Items.Add(lvi)
              End If
              ' Increase the current row counter 
              iCurrentRow += 1
            Next
            ' Add the DataTable to the DataSet 
            Exit For
          Next
          Return (lv)
        End Function
    

    If you only need to read the header, so please just delete the part of adding rows.

    I hope this will be helpful.

    Best regards,

     


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Mike Feng Wednesday, July 27, 2011 9:30 AM
    Wednesday, July 20, 2011 3:45 AM

All replies

  • Hi Amit,

    You cannot access a webpage direct in this way.

    Use for that the Agility pack or use MSHTML

    Agility pack

    http://htmlagilitypack.codeplex.com/

    MSHTML sample on our website

    http://www.vb-tips.com/dbpages.aspx?Search=mshtml

     


    Success
    Cor
    • Proposed as answer by Mike Feng Wednesday, July 20, 2011 3:09 AM
    • Marked as answer by Mike Feng Wednesday, July 27, 2011 9:30 AM
    Saturday, July 16, 2011 9:27 AM
  • Hi Amit_Kumar,

    Here is a related thread about read the HTML table to datagridview: http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/e1bb6e15-b2d9-4aa1-aa51-4a303a752b34

    In that thread, I posted the code read the HTML tables to dataset, and this code can read the HTML table to listView:

        Public Function ConvertHTMLTablesToListView(ByVal HTML As String) As ListView
          ' Declarations 
          Dim lv As New ListView
          Dim lvi As ListViewItem
          Dim TableExpression As String = "<table[^>]*>(.*?)</table>"
          Dim HeaderExpression As String = "<th[^>]*>(.*?)</th>"
          Dim RowExpression As String = "<tr[^>]*>(.*?)</tr>"
          Dim ColumnExpression As String = "<td[^>]*>(.*?)</td>"
          Dim HeadersExist As Boolean = False
          Dim iCurrentColumn As Integer = 0
          Dim iCurrentRow As Integer = 0
          ' Get a match for all the tables in the HTML 
          Dim Tables As MatchCollection = Regex.Matches(HTML, TableExpression, RegexOptions.Multiline Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)
          ' Loop through each table element 
          For Each Table As Match In Tables
            ' Reset the current row counter and the header flag 
            iCurrentRow = 0
            HeadersExist = False
            ' Create the relevant amount of columns for this table (use the headers if they exist, otherwise use default names) 
            If Table.Value.Contains("<th") Then
              ' Set the HeadersExist flag 
              HeadersExist = True
              ' Get a match for all the rows in the table 
              Dim Headers As MatchCollection = Regex.Matches(Table.Value, HeaderExpression, RegexOptions.Multiline Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)
              ' Loop through each header element 
              For Each Header As Match In Headers
                lv.Columns.Add(Header.Groups(1).ToString)
              Next
            Else
              For iColumns As Integer = 1 To Regex.Matches(Regex.Matches(Regex.Matches(Table.Value, TableExpression, RegexOptions.Multiline Or RegexOptions.Singleline Or RegexOptions.IgnoreCase).Item(0).ToString, RowExpression, RegexOptions.Multiline Or RegexOptions.Singleline Or RegexOptions.IgnoreCase).Item(0).ToString, ColumnExpression, RegexOptions.Multiline Or RegexOptions.Singleline Or RegexOptions.IgnoreCase).Count
                lv.Columns.Add("Column " & iColumns)
              Next
            End If
            ' Get a match for all the rows in the listview 
            Dim Rows As MatchCollection = Regex.Matches(Table.Value, RowExpression, RegexOptions.Multiline Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)
            ' Loop through each row element 
            For Each Row As Match In Rows
              ' Only loop through the row if it isn't a header row 
              If Not (iCurrentRow = 0 And HeadersExist = True) Then
                ' Create a new row and reset the current column counter 
                lvi = New ListViewItem
                ' Get a match for all the columns in the row 
                Dim Columns As MatchCollection = Regex.Matches(Row.Value, ColumnExpression, RegexOptions.Multiline Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)
                ' Loop through each column element 
                For Each Column As Match In Columns
                  ' Add the value to the ListViewItem 
                  lvi.SubItems.Add(Column.Groups(1).ToString)
                  ' Increase the current column  
                  iCurrentColumn += 1
                Next
                ' Add the ListViewItem to the ListView
                lv.Items.Add(lvi)
              End If
              ' Increase the current row counter 
              iCurrentRow += 1
            Next
            ' Add the DataTable to the DataSet 
            Exit For
          Next
          Return (lv)
        End Function
    

    If you only need to read the header, so please just delete the part of adding rows.

    I hope this will be helpful.

    Best regards,

     


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Mike Feng Wednesday, July 27, 2011 9:30 AM
    Wednesday, July 20, 2011 3:45 AM