question

DuBar-5641 avatar image
0 Votes"
DuBar-5641 asked JiachenLiMFST-9349 commented

Unable to add 2 or more rows in DataGridView Jsona data

I am unable to add more then 1 Row in my DataGridView I am sure code is correct but somehow it scrape only Json[0] data and the rest unable to write in next row.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
InitializeDataGridView()
End Sub

      Public Sub InitializeDataGridView()
         'Public Shared Sub InitializeDataGridView(instance As Form1)
    
         ' Form1.dgvMain.Rows.Clear()
         dgvMain.AutoGenerateColumns = False
    
         Dim textBoxCol1 As New DataGridViewTextBoxColumn
         Dim imageCol As New DataGridViewImageColumn
         imageCol.ImageLayout = DataGridViewImageCellLayout.Zoom
    
 #Region "type"
         Dim comboBoxCol As New DataGridViewComboBoxColumn
         Dim textBoxCol2 As New DataGridViewTextBoxColumn
         Dim textBoxCol3 As New DataGridViewTextBoxColumn
         Dim textBoxCol4 As New DataGridViewTextBoxColumn
         Dim textBoxCol5 As New DataGridViewTextBoxColumn
         Dim textBoxCol6 As New DataGridViewTextBoxColumn
         Dim textBoxCol7 As New DataGridViewTextBoxColumn
         Dim textBoxCol8 As New DataGridViewTextBoxColumn
         Dim textBoxCol9 As New DataGridViewTextBoxColumn
         Dim textBoxCol10 As New DataGridViewTextBoxColumn
         Dim textBoxCol11 As New DataGridViewTextBoxColumn
         Dim textBoxCol12 As New DataGridViewTextBoxColumn
         Dim textBoxCol13 As New DataGridViewTextBoxColumn
         Dim CheckBoxCol As New DataGridViewCheckBoxColumn
 #End Region
    
 #Region "columns"
         dgvMain.Columns.Add(textBoxCol1) 'Rank
         dgvMain.Columns.Add(imageCol) 'Logo Image
         dgvMain.Columns.Add(textBoxCol2) 'Symbol
         dgvMain.Columns.Add(textBoxCol3) ' Name
         'dgvMain.Columns.Add(comboBoxCol) 'Symbol
         dgvMain.Columns.Add(textBoxCol4) 'Price €
         dgvMain.Columns.Add(textBoxCol5) '1h
         dgvMain.Columns.Add(textBoxCol6) '24h
         dgvMain.Columns.Add(textBoxCol7) '7d
         dgvMain.Columns.Add(textBoxCol8) '24h Volume
         dgvMain.Columns.Add(textBoxCol9) 'Low 24h
         dgvMain.Columns.Add(textBoxCol10) 'High 24h
         dgvMain.Columns.Add(textBoxCol11) 'Market Cap
         dgvMain.Columns.Add(textBoxCol12) 'If Lower
         dgvMain.Columns.Add(textBoxCol13) 'If Higher
         dgvMain.Columns.Add(CheckBoxCol) 'Set Alarm Checkbox
 #End Region
    
 #Region "header text"
         textBoxCol1.HeaderText = "Rank"
         textBoxCol1.Width = 70
         imageCol.HeaderText = "Thumb"
         imageCol.Width = 70
         textBoxCol2.HeaderText = "Symbol"
         textBoxCol2.Width = 70
         textBoxCol3.HeaderText = "Name"
         textBoxCol3.Width = 170
         textBoxCol4.HeaderText = "Price €"
         textBoxCol4.Width = 150
         textBoxCol5.HeaderText = "1h"
         textBoxCol5.Width = 90
         textBoxCol6.HeaderText = "24h"
         textBoxCol6.Width = 90
         textBoxCol7.HeaderText = "7d"
         textBoxCol7.Width = 90
         textBoxCol8.HeaderText = "24h Volume"
         textBoxCol9.HeaderText = "Low 24h"
         textBoxCol9.Width = 100
         textBoxCol10.HeaderText = "High 24h"
         textBoxCol10.Width = 100
         textBoxCol11.HeaderText = "Market Cap"
         textBoxCol12.HeaderText = "If Lower"
         textBoxCol12.Width = 100
         textBoxCol13.HeaderText = "If Higher"
         textBoxCol13.Width = 100
         CheckBoxCol.HeaderText = "Alarm"
         CheckBoxCol.Width = 70
 #End Region
    
         dgvMain.SelectionMode = DataGridViewSelectionMode.CellSelect
    
         For Each Item As DataGridViewRow In dgvMain.Rows
             Item.Height = 30
         Next
    
         'Dim row As DataGridViewRow = CType(dgvMain.Rows(0).Clone(), DataGridViewRow)
         'row.Cells(0).Value = "XYZ"
         'row.Cells(1).Value = 50.2
         'dgvMain.Rows.Add(row)
    
         dgvMain.ClearSelection()
         dgvMain.CurrentCell = Nothing
         'dgvMain.CurrentCell.Selected = Nothing
         dgvMain.Tag = Nothing
         'dgvMain.Rows.Add()
         dgvMain.Update()
     End Sub
        
  Public Sub ScrapeBTC()
         Try
             ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
             ServicePointManager.Expect100Continue = True
             Dim _url As String = "https://api.coingecko.com/api/v3/coins/markets?vs_currency=eur&ids=bitcoin%2Cethereum%2Cbnb%2Ctether%2Ccardano%2Csolana%2Cxrp%2Cterra%2Cpolkadot%2Cdogecoin%2Cavalanche%2Cshiba-inu%2Cpolygon%near-protocol%2Calgorand%2Cdecentraland%stellar%2Cthe-sandbox%2Caxie-infinity%2Csmooth-love-potion%2Cinjective-protocol%2Cbonfida%2Cvechain%2Celrond%2Ctezos%2Chelium%2Ctheta-network%2Cmonero%2Cgala%2Ceos%2Caave%2Cenjin-coin%2Cloopring%2Cstar-atlas%2Cmoonbeam%2Cmobox%2Cooki%2Cevergrowcoin%2Cfloki-inu%2Czodium%2Csquid-game%2Cdogelon-mars%2Cholotoken%2Cstarlink%2Cbloktopia&order=market_cap_desc&per_page=100&page=1&sparkline=false&price_change_percentage=1h%2C24h%2C7d%2C"
             Dim _req As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(_url)
             _req.Proxy = Nothing
             _req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.81 Safari/537.36"
             Dim _resp As System.Net.HttpWebResponse = _req.GetResponse
             Dim streamReader As System.IO.StreamReader = New System.IO.StreamReader(_resp.GetResponseStream())
             Dim _source As String = streamReader.ReadToEnd
             ' Dim _rankResults = JsonConvert.SerializeObject(_source)
             Dim jsonResult = JsonConvert.DeserializeObject(Of List(Of JsonProperty.JsonResponse))(_source)
    
             Dim _btc_Rank As String = jsonResult(0)._rank
             Dim _btc_ThumbLink As String = jsonResult(0)._thumb
             Dim _btc_Thumb As Image = scrapeData.GetImage(_btc_ThumbLink)
             Dim _btc_Name As String = jsonResult(0)._name
             Dim _btc_Symbol As String = jsonResult(0)._symbol
             Dim _btc_PriceValue As Double = jsonResult(0)._price '
             Dim _btc_Price As String = _btc_PriceValue.ToString("N", CultureInfo.InvariantCulture).Replace(".00", "")
             Dim _btc_1h As String = jsonResult(0)._1h
             Dim _btc_24h As String = jsonResult(0)._24h
             Dim _btc_7d As String = jsonResult(0)._7d
             Dim _btc_24hValue As Double = jsonResult(0)._24volume
             Dim _btc_24volume As String = _btc_24hValue.ToString("N", CultureInfo.InvariantCulture).Replace(".00", "")
             Dim _btc_low24Value As Double = jsonResult(0)._low24
             Dim _btc_low24 As String = _btc_low24Value.ToString("N", CultureInfo.InvariantCulture).Replace(".00", "")
             Dim _btc_high24Value As Double = jsonResult(0)._high24h
             Dim _btc_high24 As String = _btc_high24Value.ToString("N", CultureInfo.InvariantCulture).Replace(".00", "")
             Dim _btc_mCapValue As Double = jsonResult(0)._mCap
             Dim _btc_mCap As String = _btc_mCapValue.ToString("N", CultureInfo.InvariantCulture).Replace(".00", "")
    
             Dim _slp_Rank As String = jsonResult(22)._rank
             Dim _slp_ThumbLink As String = jsonResult(22)._thumb
             Dim _slp_Thumb As Image = scrapeData.GetImage(_slp_ThumbLink)
             Dim _slp_Name As String = jsonResult(22)._name
             Dim _slp_Symbol As String = jsonResult(22)._symbol
             Dim _slp_PriceValue As Double = jsonResult(22)._price '
             Dim _slp_Price As String = _slp_PriceValue.ToString("N", CultureInfo.InvariantCulture).Replace(".00", "")
             Dim _slp_1h As String = jsonResult(22)._1h
             Dim _slp_24h As String = jsonResult(22)._24h
             Dim _slp_7d As String = jsonResult(22)._7d
             Dim _slp_24hValue As Double = jsonResult(22)._24volume
             Dim _slp_24volume As String = _slp_24hValue.ToString("N", CultureInfo.InvariantCulture).Replace(".00", "")
             Dim _slp_low24Value As Double = jsonResult(22)._low24
             Dim _slp_low24 As String = _slp_low24Value.ToString("N", CultureInfo.InvariantCulture).Replace(".00", "")
             Dim _slp_high24Value As Double = jsonResult(22)._high24h
             Dim _slp_high24 As String = _slp_high24Value.ToString("N", CultureInfo.InvariantCulture).Replace(".00", "")
             Dim _slp_mCapValue As Double = jsonResult(22)._mCap
             Dim _slp_mCap As String = _slp_mCapValue.ToString("N", CultureInfo.InvariantCulture).Replace(".00", "")
    
             Dim row As DataGridViewRow = Nothing
             For Each item As DataGridViewRow In dgvMain.Rows
                 row = item
                 dgvMain.Rows(0).Cells(0).Value = _btc_Rank
                 dgvMain.Rows(0).Cells(1).Value = _btc_Thumb
                 dgvMain.Rows(0).Cells(2).Value = _btc_Symbol
                 dgvMain.Rows(0).Cells(3).Value = _btc_Name
                 dgvMain.Rows(0).Cells(4).Value = _btc_Price
    
                 If _btc_1h.Contains("-") Then
                     dgvMain.Rows(0).Cells(5).Style.ForeColor = Color.Red
                 Else
                     dgvMain.Rows(0).Cells(5).Style.ForeColor = Color.Green
                 End If
                 If _btc_24h.Contains("-") Then
                     dgvMain.Rows(0).Cells(6).Style.ForeColor = Color.Red
                 Else
                     dgvMain.Rows(0).Cells(6).Style.ForeColor = Color.Green
                 End If
                 If _btc_7d.Contains("-") Then
                     dgvMain.Rows(0).Cells(7).Style.ForeColor = Color.Red
                 Else
                     dgvMain.Rows(0).Cells(7).Style.ForeColor = Color.Green
                 End If
                 dgvMain.Rows(0).Cells(5).Value = _btc_1h.Substring(0, 6) + " %"
                 dgvMain.Rows(0).Cells(6).Value = _btc_24h.Substring(0, 6) + " %"
                 dgvMain.Rows(0).Cells(7).Value = _btc_7d.Substring(0, 6) + " %"
                 dgvMain.Rows(0).Cells(8).Value = _btc_24volume
                 dgvMain.Rows(0).Cells(9).Value = _btc_low24
                 dgvMain.Rows(0).Cells(10).Value = _btc_high24
                 dgvMain.Rows(0).Cells(11).Value = _btc_mCap
                 dgvMain.Rows(1).Cells(0).Value = _slp_Rank
                 dgvMain.Rows(1).Cells(1).Value = _slp_Thumb
                 dgvMain.Rows(1).Cells(2).Value = _slp_Symbol
                 dgvMain.Rows(1).Cells(3).Value = _slp_Name
                 dgvMain.Rows(1).Cells(4).Value = _slp_PriceValue
                 If _slp_1h.Contains("-") Then
                     dgvMain.Rows(1).Cells(5).Style.ForeColor = Color.Red
                 Else
                     dgvMain.Rows(1).Cells(5).Style.ForeColor = Color.Green
                 End If
                 If _slp_24h.Contains("-") Then
                     dgvMain.Rows(1).Cells(7).Style.ForeColor = Color.Red
                 Else
                     dgvMain.Rows(1).Cells(7).Style.ForeColor = Color.Green
                 End If
                 If _slp_7d.Contains("-") Then
                     dgvMain.Rows(1).Cells(7).Style.ForeColor = Color.Red
                 Else
                     dgvMain.Rows(1).Cells(7).Style.ForeColor = Color.Green
                 End If
                 dgvMain.Rows(1).Cells(5).Value = _slp_1h.Substring(0, 6) + " %"
                 dgvMain.Rows(1).Cells(6).Value = _slp_24h.Substring(0, 6) + " %"
                 dgvMain.Rows(1).Cells(7).Value = _slp_7d.Substring(0, 6) + " %"
                 dgvMain.Rows(1).Cells(8).Value = _slp_24volume
                 dgvMain.Rows(1).Cells(9).Value = _slp_low24
                 dgvMain.Rows(1).Cells(10).Value = _slp_high24
                 dgvMain.Rows(1).Cells(11).Value = _slp_mCap
    
                 dgvMain.Rows.Add(item)
                 dgvMain.Refresh()
             Next
             _runningThreads.Remove(Thread.CurrentThread)
         Catch ex As Exception
    
         End Try
     End Sub
dotnet-standard
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @DuBar-5641 ,
Could you please provide some examples of your json data?


                 Dim row As DataGridViewRow = Nothing
                 For Each item As DataGridViewRow In dgvMain.Rows
                     row = item
                     ...
                     dgvMain.Rows.Add(item)
                     dgvMain.Refresh()
                 Next

This part of the loop in your code doesn't seem to make sense.

0 Votes 0 ·
DuBar-5641 avatar image DuBar-5641 JiachenLiMFST-9349 ·

I dont know I just newbie and copied some snippets and put together the code, the JSON data is the URL pls check

0 Votes 0 ·

Hi @DuBar-5641 ,
Thanks for your feedback.
How do you want to display your data in the DataGridView?
The JSON data you provided is an array with multiple records.
Do you want to display each record into each row of the array?

0 Votes 0 ·

0 Answers