Use Chart Control In Windows Application Using C#

We will make name vs salary chart, where we bind all the data related to salary and name from the database and show it on the chart.

Initial chamber

Step 1: Open Visual Studio 2010, Go to File, New, then Projects and under Visual C# select Windows.

window form application

You can change the name of the project and you can browse your project to different location too. And then press – OK.

Step 2: In Solution Explorer you get your Project, Add Service Based Database. By going to your Project right click and Add New Item, then select Service-based database.

Service based datyabase

Database chamber

Step 3: Go to your Database [Database.mdf], we will create a table tbl_login. Go to the database.mdf, Table, then Add New table and design your table like the following screenshot:

table design

Show table data:

table

Design chamber

Step 4: Now open your Form1.cs[Design] file, where we create our design for Chart Control Application.

We will drag Chart Control and a button from the toolbox and place it on the form. Your design looks like the following image.

drag Chart Control
Code chamber

Right click on the blank part of Form1.cs, then View Code. You will see you are entered in the code part of the form. Write the following code and then Press F5 to run the project.

Namespace for Chart Control

Namespace

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Data.SqlClient;  
  10. using System.Windows.Forms.DataVisualization.Charting;  
  11.   
  12. namespace ChartControl  
  13. {  
  14.     public partial class Form1 : Form  
  15.     {  
  16.         public Form1()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.   
  21.         private void button1_Click(object sender, EventArgs e)  
  22.         {  
  23.   
  24.             SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");  
  25.             con.Open();  
  26.             SqlCommand cmd = new SqlCommand("select * from tbl_salary", con);  
  27.             SqlDataAdapter sda = new SqlDataAdapter(cmd);  
  28.             DataSet st = new DataSet();  
  29.             sda.Fill(st, "salary");  
  30.           
  31.             chart1.DataSource = st.Tables["salary"];  
  32.             chart1.Series["Salary"].XValueMember = "name";  
  33.             chart1.Series["Salary"].YValueMembers = "salary";  
  34.             this.chart1.Titles.Add("Salary Chart for Employee");  
  35.             chart1.Series["Salary"].ChartType = SeriesChartType.Bar;  
  36.             chart1.Series["Salary"].IsValueShownAsLabel = true;  
  37.             con.Close();  
  38.   
  39.         }  
  40.     }  
  41. }  
Output chamber

Output

For Pie Chart
  1. chart1.Series["Salary"].ChartType = SeriesChartType.Pie;  
Pie Chart

For Line
  1. chart1.Series["Salary"].ChartType = SeriesChartType.Line;  
For Line

Similar way you can change in any format you want by changing the one line code. Hope you liked it.

 


Similar Articles