0

I'm so frustrated I finally managed some free API to call and display bitcoin and few more currency prices and other info in my datagridview, but when timer1_tick refresh after 5sec the data stays the same I know it made new request but not refresh the data.

ALSO when it refresh the DataGridView it creates only 1 row even my code should made 30 rows when I call it.

What I tried:

   Public Sub clear_data()
    dgvMain.Rows.Clear()
    dgvMain.Refresh()
    dgvMain.Tag = Nothing 
End Sub

    Private Sub btnUpdateChart_Click(sender As Object, e As EventArgs) Handles btnUpdateChart.Click
    Try
        scrapeData.ScrapeBonfida()
        System.Threading.Thread.Sleep(10)
        scrapeData.ScrapeSLP()
        System.Threading.Thread.Sleep(10)
        scrapeData.ScrapeBTC()

        Timer1.Enabled = True
        Timer1.Start()

    Catch ex As Exception

    End Try

End Sub

 Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

    Try

        countdown = CInt(num_refresh.Value)
        timer_countdown.Enabled = True
        Me.Timer1.Interval = CInt(Me.num_refresh.Value) * 1000
        Me.Timer1.Enabled = True

        clear_data()

        scrapeData.ScrapeBonfida()
        System.Threading.Thread.Sleep(10)
        scrapeData.ScrapeSLP()
        System.Threading.Thread.Sleep(10)
        scrapeData.ScrapeBTC()


    Catch ex As Exception

    End Try

End Sub

    Private Sub timer_countdown_Tick(sender As Object, e As EventArgs) Handles timer_countdown.Tick
    countdown -= 1

    If countdown < 0 Then
        countdown = CInt(num_refresh.Value)
        clear_data()
        CheckForIllegalCrossThreadCalls = False
        Try

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End If

End Sub

DatagridView colums:

 Public Shared Sub InitializeDataGridView()

    ' Form1.dgvMain.Rows.Clear()
    'Form1.dgvMain.AutoGenerateColumns = True

    Dim textBoxCol1 As New DataGridViewTextBoxColumn
    Dim imageCol As New DataGridViewImageColumn
    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 CheckBoxCol As New DataGridViewCheckBoxColumn


    Form1.dgvMain.Columns.Add(textBoxCol1) 'Rank
    Form1.dgvMain.Columns.Add(imageCol) 'Logo Image
    Form1.dgvMain.Columns.Add(textBoxCol2) ' Name
    Form1.dgvMain.Columns.Add(comboBoxCol) 'Symbol

    Form1.dgvMain.Columns.Add(textBoxCol3) 'Price €
    Form1.dgvMain.Columns.Add(textBoxCol4) '1h
    Form1.dgvMain.Columns.Add(textBoxCol5) '24h
    Form1.dgvMain.Columns.Add(textBoxCol6) '7d
    Form1.dgvMain.Columns.Add(textBoxCol7) '24h Volume
    Form1.dgvMain.Columns.Add(textBoxCol8) 'Low 24h
    Form1.dgvMain.Columns.Add(textBoxCol9) 'High 24h
    Form1.dgvMain.Columns.Add(textBoxCol10) 'Market Cap
    Form1.dgvMain.Columns.Add(textBoxCol11) 'If Lower
    Form1.dgvMain.Columns.Add(textBoxCol12) 'If Higher
    Form1.dgvMain.Columns.Add(CheckBoxCol) 'Set Alarm Checkbox

    textBoxCol1.HeaderText = "Rank"
    textBoxCol1.Width = 70
    imageCol.HeaderText = "Logo"
    imageCol.Width = 70
    textBoxCol2.HeaderText = "Name"
    textBoxCol2.Width = 170
    comboBoxCol.HeaderText = "Symbol"
    comboBoxCol.Width = 70
    textBoxCol3.HeaderText = "Price €"
    textBoxCol3.Width = 150
    textBoxCol4.HeaderText = "1h"
    textBoxCol4.Width = 90
    textBoxCol5.HeaderText = "24h"
    textBoxCol5.Width = 90
    textBoxCol6.HeaderText = "7d"
    textBoxCol6.Width = 90
    textBoxCol7.HeaderText = "24h Volume"
    textBoxCol8.HeaderText = "Low 24h"
    textBoxCol8.Width = 100
    textBoxCol9.HeaderText = "High 24h"
    textBoxCol9.Width = 100
    textBoxCol10.HeaderText = "Market Cap"
    textBoxCol11.HeaderText = "If Lower"
    textBoxCol11.Width = 100
    textBoxCol12.HeaderText = "If Higher"
    textBoxCol12.Width = 100
    CheckBoxCol.HeaderText = "Alarm"
    CheckBoxCol.Width = 70

    'comboBoxCol.Items.Add("BTC")
    'comboBoxCol.Items.Add("ETH")
    'comboBoxCol.Items.Add("USDT")
    Form1.dgvMain.SelectionMode = DataGridViewSelectionMode.CellSelect

    For Each Item As DataGridViewRow In Form1.dgvMain.Rows
        Item.Height = 30
    Next

    Form1.dgvMain.ClearSelection()
    Form1.dgvMain.CurrentCell = Nothing
    'Form1.dgvMain.CurrentCell.Selected = Nothing
    Form1.dgvMain.Tag = Nothing
    Form1.dgvMain.Rows.Add(30)
    Form1.dgvMain.Refresh()
End Sub
  • 1
    `CheckForIllegalCrossThreadCalls = False` means you aren't properly invoking UI calls to the UI. You should *never* need to do this in 2022 (or for the past 15 years for that matter). Pretty much same for the empty `Catch`es – djv Feb 11 '22 at 16:32
  • 1
    Also your code doesn't show the DataGridView getting populated. – djv Feb 11 '22 at 16:35
  • @djv thx for answer but I tried already without that also without try catch, nothing works sadenly – MiningChamber Feb 11 '22 at 16:36
  • 1
    Show where the datagridview is being populated. – Andrew Mortimer Feb 11 '22 at 16:36
  • Also the Timer you are using is a `System.Windows.Forms.Timer` which runs on the UI, and you should not be getting data from that thread. You have many design issues. Even the `System.Threading.Thread.Sleep(10)` is sleeping the UI. Don't do that – djv Feb 11 '22 at 16:37
  • @djv 'Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False scrapeData.InitializeDataGridView() End Sub' – MiningChamber Feb 11 '22 at 16:39
  • I can imagine a situation with a `System.Threading.Timer` which gets the data then invokes UI updates back to the UI thread. Also using a `DataSource` for the grid will simplify your code. – djv Feb 11 '22 at 16:40
  • @MiningChamber that's only called in `Form_Load`? So it only gets done once... – djv Feb 11 '22 at 16:40
  • @djv I place now initializedatagrid under button but datasource I dont know how to implement – MiningChamber Feb 11 '22 at 16:42
  • Remove `CheckForIllegalCrossThreadCalls = False` and break on the exception. Where is that? I don't see how anything is running off the UI here – djv Feb 11 '22 at 16:42
  • Ok what is `Form1` in your context? Is that an instance of `Form1`, or the [default form instance](https://stackoverflow.com/q/49948252/832052)? – djv Feb 11 '22 at 16:43
  • @djv on Form1 is button, in Class1 is shared sub which scrape data to gridview, yes I already removed, it scrape once and then nothing, I will check the breakpoint wait pls – MiningChamber Feb 11 '22 at 16:46
  • You have a class called `Form1` which is a `System.Windows.Forms.Form` which has a `System.Windows.Forms.Button` on it, ok. But in your code you have `Form1.dgvMain.Columns.Add(textBoxCol1) 'Rank ...` and it looks like your are accessing the default form instance which may not be the form you are running from. You need to retrieve the instance so it is dealing with the same `Form1` – djv Feb 11 '22 at 16:49
  • change the method declaration to `Public Shared Sub InitializeDataGridView(instance As Form1)`, and change the `Form1`s in that code to `instance`, and when calling that method change to `scrapeData.InitializeDataGridView(Me)` – djv Feb 11 '22 at 16:50
  • @djv somehow it is workign now but instead of replacing the gridview data it is adding new colums I also made 'Form1.dgvMain.AutoGenerateColumns = False' but it doesnt work – MiningChamber Feb 11 '22 at 16:51

0 Answers0