所以,我正在开发一个应用程序,我可能会遇到一些我无法解决的基本问题。我有一个连接到数据库的图表,它将运行一个查询。我能够做到这一点,但我面临的问题是它不会更新,除非我关闭并打开应用程序。
我将向您展示我正在使用的代码,然后解释它:
Public Sub UpdateChart()
Try
SQLCon = New SqlConnection
SQLCon.ConnectionString = "......................"
Timer1.Interval = 3000
Timer1.Start()
Dim sqlStatis As String = "SELECT Top 5 Filename, Filesize FROM infofile"
Dim da As New SqlDataAdapter(sqlStatis, SQLCon)
Dim ds As New DataSet()
da.Fill(ds, "infofile")
Chart1.DataSource = ds.Tables("infofile")
Catch ex As Exception
MessageBox.Show(ex.ToString())
Finally
SQLCon.Dispose()
End Try
End Sub
Public Sub BuildChart()
Dim Chart1 = New Chart()
Dim ChartArea1 As ChartArea = New ChartArea()
Dim Legend1 As Legend = New Legend()
Dim Series1 As Series = New Series()
Me.Controls.Add(Chart1)
ChartArea1.Name = "ChartArea1"
Chart1.ChartAreas.Add(ChartArea1)
Legend1.Name = "Legend1"
Chart1.Legends.Add(Legend1)
Chart1.Location = New System.Drawing.Point(12, 12)
Chart1.Name = "Chart1"
Series1.ChartArea = "ChartArea1"
Series1.Legend = "Legend1"
Series1.Name = "Tamanho do ficheiro"
Chart1.Series.Add(Series1)
Chart1.Size = New System.Drawing.Size(600, 300)
Chart1.TabIndex = 0
Chart1.Text = "Chart1"
Chart1.Series("Tamanho do ficheiro").XValueMember = "Filename"
Chart1.Series("Tamanho do ficheiro").YValueMembers = "Filesize"
End Sub
将在表单上调用此方法,数据将按我的要求显示。正如你在方法上看到的那样,我有一个Timer,它会每隔3秒或3000毫秒更新一次图表。
计时器里面我有这个:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
UpdateChart()
End Sub
在表格中我有这个:
Public Sub Gráfico_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BuildChart()
UpdateChart()
End Sub
它没有给我任何错误,但即使我创建一个名为"更新图表"的按钮,它也不会更新。并将已创建的方法放在按钮内。
那么你有什么想法我可以解决我的问题吗?
答案 0 :(得分:0)
我会搬家
Timer1.Interval = 3000
Timer1.Start()
到Form Load
和Dim Chart1
作为表单级变量,并在Load
中新增