Winform Chart Control in C# Using MS Access Database

Today I am going to show you how to chart control in C# using MS Access Database
 
Your database is shown below:
 
 
 
Now create a new project.

Select chart

View Top 5 Record

  1. private void chartcon1()  
  2. {  
  3.     if (connectiondb.State == ConnectionState.Closed)  
  4.     {  
  5.         connectiondb.Open();  
  6.     }  
  7.     str3 = "select top 5 SName,mark from tbl_result";  
  8.     com3 = new OleDbCommand(str3, connectiondb);  
  9.     OleDbDataAdapter da = new OleDbDataAdapter(com3);  
  10.     DataTable dt = new DataTable();  
  11.     da.Fill(dt);  
  12.     connectiondb.Close();  
  13.     string[] N = new string[dt.Rows.Count];  
  14.     int[] M = new int[dt.Rows.Count];  
  15.     for (int i = 0; i < dt.Rows.Count; i++)  
  16.     {  
  17.         N[i] = dt.Rows[i][0].ToString();  
  18.         M[i] = Convert.ToInt32(dt.Rows[i][1]);  
  19.     }  
  20.     chart1.Series[0].Points.DataBindXY(N, M);  
  21. }  
 
View chart By Id
  1. private void chart_by_id()  
  2. {  
  3.     if (connectiondb.State == ConnectionState.Closed)  
  4.     {  
  5.         connectiondb.Open();  
  6.     }  
  7.     str3 = "select Exam_date,mark from tbl_result where S_id='" + textBox5.Text.Trim() + "'";  
  8.     com3 = new OleDbCommand(str3, connectiondb);  
  9.     OleDbDataAdapter da = new OleDbDataAdapter(com3);  
  10.     DataTable dt = new DataTable();  
  11.     da.Fill(dt);  
  12.     connectiondb.Close();  
  13.     string[] N = new string[dt.Rows.Count];  
  14.     int[] M = new int[dt.Rows.Count];  
  15.     for (int i = 0; i < dt.Rows.Count; i++)  
  16.     {  
  17.         N[i] = dt.Rows[i][0].ToString();  
  18.         M[i] = Convert.ToInt32(dt.Rows[i][1]);  
  19.     }  
  20.     chart1.Series[0].Points.DataBindXY(N, M);  
  21. }   

And View Chart By Date change Event

  1. private void result_by_date()  
  2. {  
  3.     if (connectiondb.State == ConnectionState.Closed)  
  4.     {  
  5.         connectiondb.Open();  
  6.     }  
  7.     str3 = "select SName,mark from tbl_result where Exam_date='" + dateTimePicker2.Text.Trim() + "'";  
  8.     com3 = new OleDbCommand(str3, connectiondb);  
  9.     OleDbDataAdapter da = new OleDbDataAdapter(com3);  
  10.     DataTable dt = new DataTable();  
  11.     da.Fill(dt);  
  12.     connectiondb.Close();  
  13.     string[] N = new string[dt.Rows.Count];  
  14.     int[] M = new int[dt.Rows.Count];  
  15.     for (int i = 0; i < dt.Rows.Count; i++)  
  16.     {  
  17.         N[i] = dt.Rows[i][0].ToString();  
  18.         M[i] = Convert.ToInt32(dt.Rows[i][1]);  
  19.     }  
  20.     chart1.Series[0].Points.DataBindXY(N, M);  
  21. }  

 

This is my simple attempt at understanding chart control. Full code is shown below.

  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.OleDb;  
  10. using System.Windows.Forms.DataVisualization.Charting;  
  11. namespace Result_GRP  
  12. {  
  13.     public partial class Form1: Form  
  14.     {  
  15.         OleDbConnection connectiondb = new OleDbConnection(@  
  16.         "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\DIPANKAR\Google Drive\resultDB.accdb");  
  17.         OleDbCommand com3;  
  18.         string str3;  
  19.         public Form1()  
  20.         {  
  21.             InitializeComponent();  
  22.             chartcon1();  
  23.         }  
  24.         private void Form1_Load(object sender, EventArgs e)  
  25.         {  
  26.         }  
  27.         private void chartcon1()  
  28.         {  
  29.             if (connectiondb.State == ConnectionState.Closed)  
  30.             {  
  31.                 connectiondb.Open();  
  32.             }  
  33.             str3 = "select top 5 SName,mark from tbl_result";  
  34.             com3 = new OleDbCommand(str3, connectiondb);  
  35.             OleDbDataAdapter da = new OleDbDataAdapter(com3);  
  36.             DataTable dt = new DataTable();  
  37.             da.Fill(dt);  
  38.             connectiondb.Close();  
  39.             string[] N = new string[dt.Rows.Count];  
  40.             int[] M = new int[dt.Rows.Count];  
  41.             for (int i = 0; i < dt.Rows.Count; i++)  
  42.             {  
  43.                 N[i] = dt.Rows[i][0].ToString();  
  44.                 M[i] = Convert.ToInt32(dt.Rows[i][1]);  
  45.             }  
  46.             chart1.Series[0].Points.DataBindXY(N, M);  
  47.         }  
  48.         private void result_chart_by_date()  
  49.         {  
  50.             if (connectiondb.State == ConnectionState.Closed)  
  51.             {  
  52.                 connectiondb.Open();  
  53.             }  
  54.             str3 = "select SName,mark from tbl_result where Exam_date='" + dateTimePicker2.Text.Trim() + "'";  
  55.             com3 = new OleDbCommand(str3, connectiondb);  
  56.             OleDbDataAdapter da = new OleDbDataAdapter(com3);  
  57.             DataTable dt = new DataTable();  
  58.             da.Fill(dt);  
  59.             connectiondb.Close();  
  60.             string[] N = new string[dt.Rows.Count];  
  61.             int[] M = new int[dt.Rows.Count];  
  62.             for (int i = 0; i < dt.Rows.Count; i++)  
  63.             {  
  64.                 N[i] = dt.Rows[i][0].ToString();  
  65.                 M[i] = Convert.ToInt32(dt.Rows[i][1]);  
  66.             }  
  67.             chart1.Series[0].Points.DataBindXY(N, M);  
  68.         }  
  69.         private void chart_by_id()  
  70.         {  
  71.             if (connectiondb.State == ConnectionState.Closed)  
  72.             {  
  73.                 connectiondb.Open();  
  74.             }  
  75.             str3 = "select Exam_date,mark from tbl_result where S_id='" + textBox5.Text.Trim() + "'";  
  76.             com3 = new OleDbCommand(str3, connectiondb);  
  77.             OleDbDataAdapter da = new OleDbDataAdapter(com3);  
  78.             DataTable dt = new DataTable();  
  79.             da.Fill(dt);  
  80.             connectiondb.Close();  
  81.             string[] N = new string[dt.Rows.Count];  
  82.             int[] M = new int[dt.Rows.Count];  
  83.             for (int i = 0; i < dt.Rows.Count; i++)  
  84.             {  
  85.                 N[i] = dt.Rows[i][0].ToString();  
  86.                 M[i] = Convert.ToInt32(dt.Rows[i][1]);  
  87.             }  
  88.             chart1.Series[0].Points.DataBindXY(N, M);  
  89.         }  
  90.         private void textBox5_TextChanged(object sender, EventArgs e)  
  91.         {  
  92.             chart_by_id();  
  93.         }  
  94.         private void button1_Click(object sender, EventArgs e)  
  95.         {  
  96.         }  
  97.         private void button2_Click(object sender, EventArgs e)  
  98.         {  
  99.         }  
  100.         private void textBox1_TextChanged(object sender, EventArgs e)  
  101.         {  
  102.         }  
  103.         private void dateTimePicker2_ValueChanged_1(object sender, EventArgs e)  
  104.         {  
  105.             result_chart_by_date();  
  106.         }  
  107.     }  
  108. }  
Thanks 
Ebook Download
View all
Learn
View all