我正在从网页解析到datagridview,但它只在第一行填充。 有人可以帮我一把吗? 谢谢 代码
Dim Data(10) As String
Dim Index As Integer = 0
Dim Output As Boolean = False
For Each ELement As System.Windows.Forms.HtmlElement In WebBrowser1.Document.All
If UCase(ELement.TagName.ToString).Contains("TD") And Output = True Then
Data(Index) = ELement.InnerText
Index += 1
ElseIf UCase(ELement.TagName.ToString).Contains("TR") And Output = True Then
For Each row As DataGridViewRow In DataGridView1.Rows
row.Cells.Item("data").Value = Data(0)
row.Cells.Item("data1").Value = Data(1)
row.Cells.Item("data2").Value = Data(2)
Index = 0
Next
End If
Output = True
Next
答案 0 :(得分:1)
我修改了一下你的代码:基本上我添加了一个rowcounter
变量来跟踪你正在解析的行。
然后我创建新行,将其添加到GridView
并用值填充单元格:
Dim Data(10) As String
Dim Index As Integer = 0
Dim Output As Boolean = False
Dim rowcounter As Integer = 0
For Each ELement As System.Windows.Forms.HtmlElement In WebBrowser1.Document.All
If UCase(ELement.TagName.ToString).Contains("TD") And Output Then
Data(Index) = ELement.InnerText
Index += 1
ElseIf UCase(ELement.TagName.ToString).Contains("TR") And Output Then
If Data(0) IsNot Nothing Then
Dim row As New DataGridViewRow
DataGridView1.Rows.Add(row)
DataGridView1(0, rowcounter).Value = Data(0)
DataGridView1(1, rowcounter).Value = Data(1)
DataGridView1(2, rowcounter).Value = Data(2)
rowcounter += 1
Index = 0
End If
End If
Output = True
Next
我还在Data(0)
上添加了一项测试,以防止在没有数据时添加一行