Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i have a datagridview but the values are one under one.
I try to take one side by side.
What is the problem ?

VB
DataGridView1.ColumnCount = 2
        DataGridView1.Columns(0).Name = "Products"
        DataGridView1.Columns(1).Name = "Links"


        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
        Dim link As String = "https://www.hobbydb.com/marketplaces/hobbydb/catalog_items?utf8=%E2%9C%93&q=thanos+sideshow&commit=Go"
        Dim doc As HtmlDocument = New HtmlWeb().Load(link)
        Dim div As HtmlNode = doc.DocumentNode.SelectSingleNode("//div[@class='marketplace-collectible-list']")
        For Each Products As HtmlNode In div.SelectNodes("//div[@class='marketplace-collectible-list']//a[@class = 'catalog-item-name']")
            DataGridView1.Rows.Add(Products.InnerText.Trim())
        Next
        For Each Links As HtmlNode In div.SelectNodes("//div[@class='marketplace-collectible-list']//div[@class = 'image-container']//img//@src")
            DataGridView1.Rows.Add(Links.Attributes("src").Value)
        Next

How i do to catch all this values for datagridview like this.

DataGridView1.Rows.Add(Products.InnerText.Trim(),Links.Attributes("src").Value)


Thanks you.

What I have tried:

I try lot of tips but nothing.
Posted
Updated 8-Oct-19 22:35pm
v2
Comments
Afzaal Ahmad Zeeshan 8-Oct-19 15:25pm    
What do you mean one under one? You are adding rows, they show up one under one.
Member 12919322 9-Oct-19 3:43am    

1 solution

The reason you've got items "one-under-another" instead of "side-by-side" is that that you've created 2 separated loops:
VB.NET
'first loop - add products
For Each Products As HtmlNode In div.SelectNodes("//div[@class='marketplace-collectible-list']//a[@class = 'catalog-item-name']")
    DataGridView1.Rows.Add(Products.InnerText.Trim())
Next
'second loop - add links
For Each Links As HtmlNode In div.SelectNodes("//div[@class='marketplace-collectible-list']//div[@class = 'image-container']//img//@src")
    DataGridView1.Rows.Add(Links.Attributes("src").Value)
Next


You have to add a set of product-link at once.

As far as i see, the common part of set of product-link is: <div class="row catalog-item-info">...</div>
So, you have to loop through the collection of that divs and select single node for product, then for link.
VB
'create the datatable as a data source for the datagridview
Dim dt As DataTable = New DataTable()
dt.Columns.Add(New DataColumn("Product"))
dt.Columns.Add(New DataColumn("Link"))

'connect to the site
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
Dim siteaddress As String = "https://www.hobbydb.com/marketplaces/hobbydb/catalog_items?utf8=%E2%9C%93&q=thanos+sideshow&commit=Go"
Dim doc As HtmlDocument = New HtmlWeb().Load(siteaddress)
Dim div As HtmlNode = doc.DocumentNode.SelectSingleNode("//div[@class='marketplace-collectible-list']")
'get data
For Each item As HtmlNode In div.SelectNodes("*//div[@class = 'row catalog-item-info']")
	Dim product As HtmlNode = item.SelectSingleNode("*//a[@class = 'catalog-item-name']")
	Dim link As HtmlNode = item.SelectSingleNode("*//div[@class = 'image-container']//img//@src")
    dt.Rows.Add(New Object(){product.InnerText.Trim(), link.Attributes("src").Value})
Next

'bind data
DataGridView1.DataSource = dt


Note: Not tested! But it should works fine.

[EDIT]
To get sub-div in context of specific HtmlNode, there's a need to use "*". For further details, please see: XPath Examples | Microsoft Docs[^]

Check out updated code.

Good luck!
 
Share this answer
 
v5
Comments
Member 12919322 9-Oct-19 4:49am    
Thanks for reply. But not works like i want.
First :
With the end i have errors
dt.Rows.Add(New Object()() {product.InnerText.Trim(), link.Attributes("src").Value})

I change for:
dt.Rows.Add(product.InnerText.Trim(), link.Attributes("src").Value)

and works fine, but(see "Two")

Two:
Only take the first value and not the others (multiplicate the first value by select For Each).
It's maybe because :
Dim div As HtmlNode = doc.DocumentNode.SelectSingleNode("//div[@class='marketplace-collectible-list']")
Maciej Los 9-Oct-19 4:53am    
Check updated answer. I've made few minor errors, which were corrected few seconds ago.
Member 12919322 9-Oct-19 5:14am    
Maciej Los, Do you my comment ?
Maciej Los 9-Oct-19 6:12am    
Please, see updated answer (and code).
Member 12919322 9-Oct-19 6:15am    
Thanks for the help. Works very fine. Now i have a good exemple.
Thanks you a lot Maciej Los.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900