简体   繁体   中英

VB.net datagridview to chart control devexpress

I have a study about passing DataGridView values to DevExpress ChartControl. I have X and Y values in my DataGridView (It can have different row count). I would like to use for next loop due to different point counts (needs to stop after last value). Sometimes I have 5 values, sometimes 8, 12, ... etc. I have use code below:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim i As Integer

    For i = 0 To DataGridView1.Rows.Count - 1

        ChartControl1.Series("Series 1").Points.Add(New SeriesPoint(DataGridView1.Item(0, i).Value, DataGridView1.Item(1, i).Value))
    Next

End Sub

See also picture:
在此处输入图片说明

There is the new row in your DataGridView . You need to check for new row before adding the values from current row to your chart. To do this you can use the DataGridView.NewRowIndex property.
Here is example:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim i As Integer

    For i = 0 To DataGridView1.Rows.Count - 1
        If i <> DataGridView1.NewRowIndex Then
            ChartControl1.Series("Series 1").Points.Add(New SeriesPoint(DataGridView1.Item(0, i).Value, DataGridView1.Item(1, i).Value))
        End If
    Next

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM