Windows Forms: How to Link Chart /Graph with Database in C#
By FoxLearn 6/6/2017 9:15:06 PM 15.85K
How to Link Chart/Graph with Local Database in C# using DataBinding
Step 1: Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "ChartDemo" and then click OK
Step 2: Create a local database, then add a AgeStatistic table to your database -> create a dataset then drag the AgeStatistic table to your dataset as below
Step 3: Design your form as below
Step 4: Add code to handle your form as below
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ChartDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'database.AgeStatistics' table. You can move, or remove it, as needed. this.ageStatisticsTableAdapter.Fill(this.database.AgeStatistics); } private void btnSave_Click(object sender, EventArgs e) { try { //Update data to sql database ageStatisticsBindingSource.EndEdit(); ageStatisticsTableAdapter.Update(database.AgeStatistics); MessageBox.Show("Your data has been successfully saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch(Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void btnLoad_Click(object sender, EventArgs e) { //Load data to chart/graph chart1.Series["Age"].XValueMember = "Age"; chart1.Series["Age"].YValueMembers = "Total"; chart1.DataSource = database.AgeStatistics; chart1.DataBind(); } } }
VIDEO TUTORIALS
- How to save files using SaveFileDialog in C#
- How to make an Alarm clock with sound in C#
- How to Display Images in DataGridView in C#
- How to Print DataGridView with Header & Footer with Landscape in C#
- How to Create a custom Progress Bar with Percentage in C#
- How to read an image file in C#
- How to use BackgroundWorker in C#
- How to protect .NET code from reverse engineering
Categories
Popular Posts
Admin Tailwind CSS Admin Dashboard Template
11/18/2024
Regal Admin Dashboard Template
11/18/2024
Flat Able Admin Dashboard Template
11/18/2024
AdminKit Bootstrap 5 HTML5 UI Kits Template
11/17/2024