Chart control in C# window application.


Chart: The chart control is used to display your data as a Chart like Pie chart, Line Chart, Point chart, Column, Bar & Area chart. The Chart control visualizes your data. 

In this article, we will learn how to use chart control in window application using c#. We will learn how to bind chart control to database.
In this article I will show data in chart control from database. We can type of chart control in properties of series added. Chart can be of any type: Column. Bar(Stacked Bar), Line, Point, pie chart, Area chart etc.

Follow these steps:

  1.       Add a window form in application.
  2.       Place a chart control on form and Add series to chart control.
Chart control ->Properties-> Series-> Add series (Give name to series)
  3.       Now bind chart control to database using code in code behind.
  4.       Set X-Value member and Y-value member in chart control series.
  5.       Now execute the form.

Create a table in database:









Now place chart control and add series


Now Add Series to Chart Control:



Now Right following code in code behind file on Page Load Event

Add Following Name Spaces:

using System.Data.SqlClient;
using System.Configuration;


Full code:

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;
using System.Data.SqlClient;
using System.Configuration;
namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }    

        private void Form3_Load(object sender, EventArgs e)
        {
            DataTable dt = new System.Data.DataTable();
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
            con.Open();
            SqlDataAdapter adp = new SqlDataAdapter("Select * from Employee", con);
            adp.Fill(dt);
            chart1.DataSource = dt;
            chart1.Series["Salary"].XValueMember = "Name";
            chart1.Series["Salary"].YValueMembers = "Salary";
            chart1.Titles.Add("Salary chart");
            con.Close();
        }

    }
}



Kategori

Kategori