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
- private void chartcon1()
- {
- if (connectiondb.State == ConnectionState.Closed)
- {
- connectiondb.Open();
- }
- str3 = "select top 5 SName,mark from tbl_result";
- com3 = new OleDbCommand(str3, connectiondb);
- OleDbDataAdapter da = new OleDbDataAdapter(com3);
- DataTable dt = new DataTable();
- da.Fill(dt);
- connectiondb.Close();
- string[] N = new string[dt.Rows.Count];
- int[] M = new int[dt.Rows.Count];
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- N[i] = dt.Rows[i][0].ToString();
- M[i] = Convert.ToInt32(dt.Rows[i][1]);
- }
- chart1.Series[0].Points.DataBindXY(N, M);
- }
View chart By Id
- private void chart_by_id()
- {
- if (connectiondb.State == ConnectionState.Closed)
- {
- connectiondb.Open();
- }
- str3 = "select Exam_date,mark from tbl_result where S_id='" + textBox5.Text.Trim() + "'";
- com3 = new OleDbCommand(str3, connectiondb);
- OleDbDataAdapter da = new OleDbDataAdapter(com3);
- DataTable dt = new DataTable();
- da.Fill(dt);
- connectiondb.Close();
- string[] N = new string[dt.Rows.Count];
- int[] M = new int[dt.Rows.Count];
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- N[i] = dt.Rows[i][0].ToString();
- M[i] = Convert.ToInt32(dt.Rows[i][1]);
- }
- chart1.Series[0].Points.DataBindXY(N, M);
- }
And View Chart By Date change Event
- private void result_by_date()
- {
- if (connectiondb.State == ConnectionState.Closed)
- {
- connectiondb.Open();
- }
- str3 = "select SName,mark from tbl_result where Exam_date='" + dateTimePicker2.Text.Trim() + "'";
- com3 = new OleDbCommand(str3, connectiondb);
- OleDbDataAdapter da = new OleDbDataAdapter(com3);
- DataTable dt = new DataTable();
- da.Fill(dt);
- connectiondb.Close();
- string[] N = new string[dt.Rows.Count];
- int[] M = new int[dt.Rows.Count];
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- N[i] = dt.Rows[i][0].ToString();
- M[i] = Convert.ToInt32(dt.Rows[i][1]);
- }
- chart1.Series[0].Points.DataBindXY(N, M);
- }
This is my simple attempt at understanding chart control. Full code is shown below.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.OleDb;
- using System.Windows.Forms.DataVisualization.Charting;
- namespace Result_GRP
- {
- public partial class Form1: Form
- {
- OleDbConnection connectiondb = new OleDbConnection(@
- "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\DIPANKAR\Google Drive\resultDB.accdb");
- OleDbCommand com3;
- string str3;
- public Form1()
- {
- InitializeComponent();
- chartcon1();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void chartcon1()
- {
- if (connectiondb.State == ConnectionState.Closed)
- {
- connectiondb.Open();
- }
- str3 = "select top 5 SName,mark from tbl_result";
- com3 = new OleDbCommand(str3, connectiondb);
- OleDbDataAdapter da = new OleDbDataAdapter(com3);
- DataTable dt = new DataTable();
- da.Fill(dt);
- connectiondb.Close();
- string[] N = new string[dt.Rows.Count];
- int[] M = new int[dt.Rows.Count];
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- N[i] = dt.Rows[i][0].ToString();
- M[i] = Convert.ToInt32(dt.Rows[i][1]);
- }
- chart1.Series[0].Points.DataBindXY(N, M);
- }
- private void result_chart_by_date()
- {
- if (connectiondb.State == ConnectionState.Closed)
- {
- connectiondb.Open();
- }
- str3 = "select SName,mark from tbl_result where Exam_date='" + dateTimePicker2.Text.Trim() + "'";
- com3 = new OleDbCommand(str3, connectiondb);
- OleDbDataAdapter da = new OleDbDataAdapter(com3);
- DataTable dt = new DataTable();
- da.Fill(dt);
- connectiondb.Close();
- string[] N = new string[dt.Rows.Count];
- int[] M = new int[dt.Rows.Count];
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- N[i] = dt.Rows[i][0].ToString();
- M[i] = Convert.ToInt32(dt.Rows[i][1]);
- }
- chart1.Series[0].Points.DataBindXY(N, M);
- }
- private void chart_by_id()
- {
- if (connectiondb.State == ConnectionState.Closed)
- {
- connectiondb.Open();
- }
- str3 = "select Exam_date,mark from tbl_result where S_id='" + textBox5.Text.Trim() + "'";
- com3 = new OleDbCommand(str3, connectiondb);
- OleDbDataAdapter da = new OleDbDataAdapter(com3);
- DataTable dt = new DataTable();
- da.Fill(dt);
- connectiondb.Close();
- string[] N = new string[dt.Rows.Count];
- int[] M = new int[dt.Rows.Count];
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- N[i] = dt.Rows[i][0].ToString();
- M[i] = Convert.ToInt32(dt.Rows[i][1]);
- }
- chart1.Series[0].Points.DataBindXY(N, M);
- }
- private void textBox5_TextChanged(object sender, EventArgs e)
- {
- chart_by_id();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- }
- private void button2_Click(object sender, EventArgs e)
- {
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- }
- private void dateTimePicker2_ValueChanged_1(object sender, EventArgs e)
- {
- result_chart_by_date();
- }
- }
- }
Thanks