Click here to Skip to main content
14,330,663 members
Rate this:
Please Sign up or sign in to 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 ?

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
   
I have that
https://ibb.co/yNbQxHQ
But i want that.
https://ibb.co/DVWPSC9

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 2

The reason you've got items "one-under-another" instead of "side-by-side" is that that you've created 2 separated loops:
'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.
'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!
   
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.
Maciej Los 9-Oct-19 6:16am
   
The best "Thanks" is when you accept (using green button) and upvote (using voting system - on the right-top corner) my answer.
Member 12919322 9-Oct-19 7:17am
   
Done my Virtual Heroe !
Maciej Los 9-Oct-19 7:21am
   
Thank you.
Cheers!
Maciej

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100