In this article I will explain with an example, how to create Bar Chart in Windows Forms (WinForms) Application using C# and VB.Net.
The Bar Chart will be populated from SQL Server database in Windows Forms (WinForms) Application using C# and VB.Net.
 
 
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 
The Chart control
The Chart control is present in the Data section of the Visual Studio ToolBox.
Create Bar Chart in Windows Forms Application using C# and VB.Net
 
 
Form Design
The Form consists of a Chart control.
Create Bar Chart in Windows Forms Application using C# and VB.Net
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms.DataVisualization.Charting;
 
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Windows.Forms.DataVisualization.Charting
 
 
Populating Bar Chart from database in Windows Forms Application
Inside the Form Load event of the page, the Bar Chart is populated from database.
The SQL Query gets the City-wise Order distribution and the records from database are populated into a DataTable.
The names of the Cities are copied to the X array while the Order Totals for the corresponding cities are copied to the Y array.
Finally the X and Y arrays are assigned to the Chart control.
C#
private void Form1_Load(object sender, EventArgs e)
{
    //Fetch the Statistical data from database.
    string query = "SELECT ShipCity, COUNT(OrderId) [Total]";
    query += " FROM Orders WHERE ShipCountry = 'Brazil'";
    query += " GROUP BY ShipCity";
    DataTable dt = GetData(query);
 
    //Get the names of Cities.
    string[] x = (from p in dt.AsEnumerable()
                  orderby p.Field<string>("ShipCity") ascending
                  select p.Field<string>("ShipCity")).ToArray();
 
    //Get the Total of Orders for each City.
    int[] y = (from p in dt.AsEnumerable()
               orderby p.Field<string>("ShipCity") ascending
               select p.Field<int>("Total")).ToArray();
 
    Chart1.Series[0].LegendText = "Brazil Order Statistics";
    Chart1.Series[0].ChartType = SeriesChartType.Bar;
    Chart1.Series[0].IsValueShownAsLabel = true;
    Chart1.Series[0].Points.DataBindXY(x, y);
}
 
private static DataTable GetData(string query)
{
    string constr = @"Data Source=.\SQL2005;Initial Catalog=Northwind;User ID=sa;Password=pass@123";
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
        {
            DataTable dt = new DataTable();
            sda.Fill(dt);
            return dt;
        }
    }
}
 
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    'Fetch the Statistical data from database.
    Dim query As String = "SELECT ShipCity, COUNT(OrderId) [Total]"
    query &= " FROM Orders WHERE ShipCountry = 'Brazil'"
    query &= " GROUP BY ShipCity"
 
    Dim dt As DataTable = GetData(query)
 
    'Get the names of Cities.
    Dim x As String() = (From p In dt.AsEnumerable() _
                         Order By p.Field(Of String)("ShipCity") _
                         Select p.Field(Of String)("ShipCity")).ToArray()
 
    'Get the Total of Orders for each City.
    Dim y As Integer() = (From p In dt.AsEnumerable() _
                          Order By p.Field(Of String)("ShipCity") _
                          Select p.Field(Of Integer)("Total")).ToArray()
 
    Chart1.Series(0).LegendText = "Brazil Order Statistics"
    Chart1.Series(0).ChartType = SeriesChartType.Bar
    Chart1.Series(0).IsValueShownAsLabel = True
    Chart1.Series(0).Points.DataBindXY(x, y)
End Sub
 
Private Shared Function GetData(ByVal query As String) As DataTable
    Dim constr As String = "Data Source=.\SQL2005;Initial Catalog=Northwind;User ID=sa;Password=pass@123"
    Using con As SqlConnection = New SqlConnection(constr)
        Using sda As SqlDataAdapter = New SqlDataAdapter(query, con)
            Dim dt As DataTable = New DataTable()
            sda.Fill(dt)
            Return dt
        End Using
    End Using
End Function
 
 
Screenshot
Create Bar Chart in Windows Forms Application using C# and VB.Net
 
 
Downloads