Home > DeveloperSection > Articles > Working with chart in C#

Working with chart in C#

C# C# 
0 Comment(s)
 8413  View(s)
Rate this:

Working with chart in C#

In this article I am trying to make a small demo and explain the concept of chart in C#.


Chart is a graphical representation of data, charts allow users to see what the result of various data is to better understand and predict current and future data.

Steps for creating demo project:-

1.       Create a new project New >> Project (give the appropriate name of project).

2.       Now by default show a form1, in form1 drag and drop a datagridview where add the information for chart.

3.       After added datagridview drag and drop the chart from toolbox.

4.       After adding chart Goto on its property and click on the series property open its dialog box and rename the series (I rename the series into StudentScore).

Working with chart in C#

5.       Now double click on the form1 and get the form load event.

6.       After getting form load event write the code in the form load event.

1.  private void Form2_Load(object sender, EventArgs e)

2.  {

3.  try

4.  {

5.  dataGridView1.Rows.Add("alex stewart", 24, 56);

6.  dataGridView1.Rows.Add("chris harris", 34, 76);

7.  dataGridView1.Rows.Add("frank smith", 21, 46);

8.  dataGridView1.Rows.Add("henry paul", 27, 66);

9.  dataGridView1.Rows.Add("lan bishop", 31, 59);

10. for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)

11. {                  this.chart1.Series["StudentScore"].Points.AddXY(dataGridView1.Rows[i].Cells[0].Value.ToString(), Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value.ToString()));

12. }

13. }

14. catch (Exception ex)

15. {

16. MessageBox.Show("something is wrong");

17. }

18. }


Code Description:-

1.       This is form load event.

5.       To  9. Add data in datagridview.

10.   This is forloop it will run while datagridview rows count.

11.   This line of code will add data in chart from datagridview.


Working with chart in C#


Don't want to miss updates? Please click the below button!

Follow MindStick