VB DataGridView populating with Betfair data but unable to Graph price changes

9 Views Asked by At

I am new to this so excuse my ignorants if the terminology I use is not the usual!

Visual Basic: I am trying to take Betfair price data which I have populating a DataGridView in to a Graph i.e. I click a 'selectionId' in the DataGridView and I am trying to get the changes in that row for the 'back' column which holds the price to plot to Graph. Currently the code below is performing to the point it plots the first price showing in the DataGridView but doesn't reflect subsequent changes that are taking place every 5 seconds in the DataGridView. Any help would be appreciated.

Private Sub DataGridView_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick

    ' Check if the click is on a row, not the column header
    If e.RowIndex >= 0 AndAlso DataGridView1.Columns(e.ColumnIndex).Name = "selectionId" Then

        ' Get the 'runnerName' and set it as the chart title
        Dim runnerName As String = DataGridView1.Rows(e.RowIndex).Cells("runnerName").Value.ToString()
        Chart1.Titles.Clear()
        Chart1.Titles.Add(runnerName)

        ' Get the 'selectionId' from the clicked row
        Dim selectionId As String = DataGridView1.Rows(e.RowIndex).Cells("selectionId").Value.ToString()


        ' Initialize the series for the chart
        Chart1.Series.Clear()
        Dim series As New Series("backPrice")
        series.ChartType = SeriesChartType.Line
        Chart1.Series.Add("backPrice")

        ' Store the date-stamped 'back' values
        Dim backPrice As New List(Of KeyValuePair(Of DateTime, Double))

        ' Loop through the DataGridView rows to find the row with the same 'selectionId'
        For Each row As DataGridViewRow In DataGridView1.Rows
            If row.Cells("selectionId").Value.ToString() = selectionId Then
                ' Get the 'back' value to plot on the Y-Axis
                Dim backValues As Double = Double.Parse(row.Cells("back").Value.ToString())
                ' Get the current time for the X-Axis
                Dim timeStamp As DateTime = DateTime.Now

                ' Add the point to the chart series
                Chart1.Series("backPrice").Points.AddXY(timeStamp.ToOADate(), backValues)

                ' Store the date-stamped 'back' value
                backPrice.Add(New KeyValuePair(Of DateTime, Double)(timeStamp, backValues))
            End If
        Next

        ' Adjust the chart area to accommodate the plotted values dynamically
        Dim chartArea As ChartArea = Chart1.ChartAreas(0)
        chartArea.AxisX.LabelStyle.Format = "HH:mm:ss"
        chartArea.AxisX.IntervalType = DateTimeIntervalType.Seconds
        chartArea.AxisX.Interval = 0.1
        chartArea.RecalculateAxesScale()

    End If

End Sub   

I have also tried including another sub using DataGridView_CellValueChanged with a similar result

0

There are 0 best solutions below