将列表项添加到数据网格视图

时间:2013-12-28 15:08:37

标签: vb.net

在我的一个程序中我正在从网页上抓几个数据并将其添加到列表中有七个列表,其中添加了数据,我想将所有七个列表项添加到数据网格视图中我试过的不同列以下代码但只有一行被添加到数据网格

For Each link As HtmlNode In doc.DocumentNode.SelectNodes("//a[@class='job-title']")
    ans = link.InnerText
    list.Add(ans)
Next
For Each link1 As HtmlNode In doc.DocumentNode.SelectNodes("//em[@class='advertiser-name']")
    ans1 = link1.InnerText
    list1.Add(ans1)
Next
For Each link2 As HtmlNode In doc.DocumentNode.SelectNodes("//div[@class='classification']")
    ans2 = link2.InnerText
    list2.Add(ans2)
Next

For Each link3 As HtmlNode In doc.DocumentNode.SelectNodes("//span[@class='listing-date']")
    ans3 = link3.InnerText
    list3.Add(ans3)
Next

For Each link4 As HtmlNode In doc.DocumentNode.SelectNodes("//span[@class='location']")
    ans4 = link4.InnerText
    list4.Add(ans4)
Next

For Each link5 As HtmlNode In doc.DocumentNode.SelectNodes("//span[@class='salary-range']")
    ans5 = link5.InnerText
    list5.Add(ans5)
Next

For Each link6 As HtmlNode In doc.DocumentNode.SelectNodes("//a[@class='job-title']")
    ans6 = link6.Attributes("href").Value
    list6.Add(ans6)

Next
    adddata()


Private Function adddata()
    Dim i As Integer
    Do Until list.Count = -1
        Form1.dgv.Rows.Add(list.Item(i), list1.Item(i), list2.Item(i), list3.Item(i), list4.Item(i), list5.Item(i), list6.Item(i))
    Loop
End Function 

我是vb.net的新手,所以请不要介意

1 个答案:

答案 0 :(得分:0)

您的adddata功能逻辑看起来有些偏差。我想这就是你想要的:

Private Function adddata(ByVal list As List(Of String))
    Dim i As Integer = list.Count - 1
    Do Until i = -1
        Form1.dgv.Rows.Add(list.Item(i), list1.Item(i), list2.Item(i), list3.Item(i), list4.Item(i), list5.Item(i), list6.Item(i))
        i = i-1
    Loop
End Function 

为每个列表调用该函数(list,list1,list2,...),将列表作为函数的参数传递。