Let's Begin
Step 1
Open Visual Studio (I am using Visual Studio 2012) and create a new Windows Forms Application
(select .NET Framework 4).
Step 2
Drop a Chart control from the Toolbox present in the Data tab.
Step 3
Go to Chart properties then click on Series.
Change the name of Series. Here, I set the name to Salary. We can also change the ChartType as
well as appearance from the Series Collection Editor.
Go to the [Link] code and add the following lines of code:
1. using System;
2. using [Link];
3.
4. namespace DemoChart
5. {
6. public partial class Form1 : Form
7. {
8. public Form1()
9. {
10. InitializeComponent();
11. }
12.
13. private void Form1_Load(object sender, EventArgs e)
14. {
15. fillChart();
16. }
17. //fillChart method
18. private void fillChart()
19. {
20. //AddXY value in chart1 in series named as Salary
21. [Link]["Salary"].[Link]("Ajay", "10000");
22. [Link]["Salary"].[Link]("Ramesh", "8000");
23. [Link]["Salary"].[Link]("Ankit", "7000");
24. [Link]["Salary"].[Link]("Gurmeet", "10000");
25. [Link]["Salary"].[Link]("Suresh", "8500");
26. //chart title
27. [Link]("Salary Chart");
28. }
29. }
30. }
Preview
Displaying Data in Chart control from the Database in Windows Forms Application
Step 1
Create a database (named Sample). Add a Table tbl_EmpSalary. The following is the table schema
for creating tbl_EmpSalary.
Use the following preceding procedure. Add these lines of code to [Link]:
1. using System;
2. using [Link];
3. using [Link];
4. using [Link];
5.
6. namespace DemoChart
7. {
8. public partial class Form1 : Form
9. {
10. public Form1()
11. {
12. InitializeComponent();
13. }
14.
15. private void Form1_Load(object sender, EventArgs e)
16. {
17. fillChart();
18. }
19. //fillChart method
20. private void fillChart()
21. {
22. SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Sample
;Integrated Security=true;");
23. DataSet ds = new DataSet();
24. [Link]();
25. SqlDataAdapter adapt = new SqlDataAdapter("Select Name,Salary from tbl_EmpS
alary", con);
26. [Link](ds);
27. [Link] = ds;
28. //set the member of the chart data source used to data bind to the X-
values of the series
29. [Link]["Salary"].XValueMember = "Name";
30. //set the member columns of the chart data source used to data bind to the
X-values of the series
31. [Link]["Salary"].YValueMembers = "Salary";
32. [Link]("Salary Chart");
33. [Link]();
34.
35. }
36. }
37. }
Final Preview