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("series1").ChartType = SeriesChartType.Line
chart1.DataSource = bsViewStock
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.
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.
- 已提議為解答 DelianTMicrosoft employee, Moderator 2009年2月16日 下午 11:18
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.
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 GorevMicrosoft employee, Owner 2009年2月21日 上午 02:07
Dear 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.
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?
If TmrVal = 10 Then
TmrVal = 0
Me.Chart1.DataBind() 'refresh Chart1?