VB.NET如何重用/重新查询/重置/清除现有图表以显示新数据

时间:2016-11-05 18:46:48

标签: vb.net charts

我正在尝试重复使用我的图表,但我收到了错误

  

在'SeriesCollection'中找不到名为'Series1'的图表元素。

每次我试着重新查询。我试着在调用我的函数之前添加这3行代码,但没有用,有什么想法吗?

Chart1.DataSource = Nothing Chart1.Series.Clear() Chart1.ChartAreas.Clear()

我的代码:

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   'still getting the same error
   'Chart1.DataSource = Nothing
   'Chart1.Series.Clear()
   'Chart1.ChartAreas.Clear()
   GetData()
End Sub

Private Sub GetData()
    If Not cnninventory.State = ConnectionState.Open Then
        cnninventory.Open()
    End If
    Dim d1 As DateTime = DateTimePicker1.Value
    Dim d2 As DateTime = DateTimePicker2.Value
    Dim searchsql1 As New OleDb.OleDbDataAdapter("Select * from saleshisTBL where HistDate >= #" & String.Format("{0:MM/dd/yyyy}", d1) & "# and HistDate <= #" & String.Format("{0:MM/dd/yyyy}", d2) & "# ORDER BY HistDate ", cnninventory)
    Dim ds As New DataSet
    searchsql1.Fill(ds, "saleshisTBL")
    Chart1.DataSource = ds.Tables("saleshisTBL")
    Dim Series1 As Series = Chart1.Series("Series1")
    Series1.Name = "Sales"
    Chart1.Series(Series1.Name).XValueMember = "ItemSold"
    Chart1.Series(Series1.Name).YValueMembers = "Quantity"
End Sub

1 个答案:

答案 0 :(得分:2)

尝试下面,它也适用于你

Private Sub GetData()
    If Not cnninventory.State = ConnectionState.Open Then
        cnninventory.Open()
    End If
    Dim d1 As DateTime = DateTimePicker1.Value
    Dim d2 As DateTime = DateTimePicker2.Value
    Dim searchsql1 As New OleDb.OleDbDataAdapter("Select * from saleshisTBL where HistDate >= #" & String.Format("{0:MM/dd/yyyy}", d1) & "# and HistDate <= #" & String.Format("{0:MM/dd/yyyy}", d2) & "# ORDER BY HistDate ", cnninventory)
    Dim ds As New DataSet
    searchsql1.Fill(ds, "saleshisTBL")
    Chart1.DataSource = ds.Tables("saleshisTBL")

    Chart1.Series.Clear()
    Dim Series1 As New DataVisualization.Charting.Series
    With Series1
        .Name = "Sales"
        .ChartType = SeriesChartType.Bar
        .XValueMember = "ItemSold"
        .YValueMembers = "Quantity"
    End With
    Chart1.Series.Add(Series1)
    Chart1.Invalidate()

End Sub