Click here to Skip to main content
15,918,516 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a lot of charts, is it possible to loop through to initialise / set data? right now i have


etc. for multiple series and multiple charts. i'd love to loop through but i can't seem to set the text using a loop


i just get an error. i've also tried the loop below, and whilst i get no errors, nothing happens

it probably matters that the charts are located in split panels, which are located in tabs. so there are two charts per tab for 14 tabs.


What I have tried:

foreach (Chart x in this.Controls.OfType<chart>())
Updated 27-Oct-16 5:14am
Member 12815488 27-Oct-16 22:33pm    
thanks! that worked. appreciate it

1 solution

An issue causing the error when you did the Chart[i].Series[i].Points.Clear() is most likely caused by using the same iterator for Chart and Series. This would only ever work if you had the same number of each Chart and Series.

Also the loop posted only clears points, have you debugged through to ensure it is getting inside the loop?

So while looping through the tabs as I'm assuming the loop you provided does, you will need 2 additional for/foreach loops.

Your loop will iterate each Chart control found.
Then one loop will iterate each ChartArea within the Chart loop, and the second should iterate each Series within the ChartArea loop.

foreach (Chart x in this.Controls.OfType(Chart)) {
    foreach (ChartArea area in x.ChartAreas) {
        foreach (Series series in area.Series){

             // Add new points here

You should add to ensure that the ChartAreas and Series collections are not null!
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900