Chart update/refresh - How to do it ?
-
2009年2月16日 下午 03:37
Dear Friends,
I am using the MS chart on winforms and use MS Access database for data. Display also works fine. Question is when i try to update the data and display the chart, i get the error:
chart1.series("Series1") .. exists already.
Here is the code i use to display the chart:
chart1.Series.Add("series1")
chart1.Series("series1").ChartType = SeriesChartType.Line
chart1.ChartAreas.Add("series1")chart1.DataSource = bsViewStock
Me.taViewStock.Fill(Me.DsViewStock.tblMain)
chart1.DataBind()
and after some time, when i press a Button to refresh or update, i get the error.
My question is:
1. How do i refresh the chart when the data is updated ?
2. How do i use the same chart to display data for another series, the old series should disappear?
thanks for your replies.
senthil
所有回覆
-
2009年2月16日 下午 11:13版主
Hi Senthil,
You are on right way: you have to call Chart.DataBind() from you code when the data is ready.
The Chart.DataBind() doesn't create new series - just the series data points will be cleared and populated from the data.
You have to create needed series in you code, depending on your data ( don't forget for Series.X(Y)ValueMember properties to be set before DataBind()) .
Please consider using Chart.DataBindTable which creates series automatically.
Thanks,
Delian- 已提議為解答 DelianTMicrosoft Employee, Moderator 2009年2月16日 下午 11:18
-
2009年2月17日 上午 05:08Mr Delian,
Thanks for the reply.
Perhaps i should explain a bit more about my project. There is adatagrid which collects information from internet and displays them every 1 min. These values are also stored to access db every min. Now, i use the datagrid cellcontent double click event to get the cell value and use that value to fetch the rows from the database using a filter and display the contents in the chart.
As you can imagine, the same cell may be dbl-clicked or the next row might be dbl-clicked. Clicking on a new row is not a problem, it displays the chart (but smaller in size). But when the same cell is dbl-clicked, the error pops up.
I want to over come this and show the graph whether the same cell or a new cell is double clicked. I put all the code, cell to string, dataset filtering, filling the dataset and charting, in the cell double-clicked event.
Perhaps you can show a better way or how to achieve what i want.
thanks
senthil -
2009年2月21日 上午 02:07擁有者
There are lots of details about your application that we do not see, so i would like to go back to the original error: "Series already exists"...
This error means that you adding a series to the chart and the series with this name already exists in the collection. Keep in mind that you maybe adding series in the chart indirectly through binding. You may just need to clean-up all the series in the chart at some point...
Alex.
http://blogs.msdn.com/alexgor- 已提議為解答 Alex GorevMicrosoft Employee, Owner 2009年2月21日 上午 02:07
-
2009年2月21日 上午 05:12Dear Mr Alex,
Thanks for the reply. This application is basically a stock quote application which displays quote values in a datagrid. The grid values are changing every 5 sec and at the end of a minute, the values are stored in a Access database.
The way i make up the chart is like this: There is a datagridview with rows of data. When the first column cell is double clicked, i pass the value of the cell to a variable, apply a filter to a BindingSource, fill the tableadapter and the result is displayed in a grid.
Upto now the charts is shown on a first double click. In the first post, the 'series1' actually is the stock code. So when i want to see the chart of another cell, when i double click the cell, this error pops up. Also when i want to see the updated value of the same cell also this error comes up.
I looked at the Dispose() and Invalidate() functions, but couldnt really work out a solution. And there is no chart1.series("series1).remove kind of function available.
I looked for a code that can, clear the series first, assign a new value to series, filter on new condition and databind. I have used Zedgraph for some time now, and this handles the issue quite nicely without any error. However, now that i want to implement advanced features, i am trying to use the MS graph.
Please shed some light.
Senthil. -
2009年2月21日 上午 06:44擁有者
You can remove all series from the chart like this: chart.Series.Clear();
You can also remove an individual series if needed.
Alex.
http://blogs.msdn.com/alexgor- 已標示為解答 kesk 2009年2月21日 上午 11:26
-
2009年2月21日 上午 11:26Mr Alex,
It works. Thanks for the same. I used the following 2 lines of code before the chart.series("series1").add :
chart.series.clear()
chart.chartarea.clear()
and now the functionality is working as expected.
Senthil -
2009年11月27日 下午 01:02I have multiple reports being generated from the same data binding source, and I needed to be able to update the chart each time I changed the report or updated the data. Chart.DataBind() was exactly what I needed to do that and it works perfectly. Thank you big-time!
-
2012年8月17日 下午 12:10
Hallo MAXIMUM_MIKE!
I should like to take advantage of your advice, but it doesn't work. Below is my code example. If you change the chart table is not updated. What's the matter?
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
SVal = SVal + 1
EVal = EVal + 1
RVal = Rnd()
NVal = 10 * RVal
Me.NTableAdapter1.Insert(SVal, 0, EVal, NVal) 'database update
If NVal = 10 Then
Timer1.Enabled = False
Me.Chart1.DataBind() 'refresh Chart1?
Else
If TmrVal = 10 Then
TmrVal = 0
Me.Chart1.DataBind() 'refresh Chart1?
End If
End If
End Sub- 已提議為解答 TV66 2012年8月17日 下午 12:10
- 已取消提議為解答 siplaModerator 2012年8月20日 下午 12:48

