In this article I will explain with an example, how to create Multi-Series Line Chart (Graph) in Windows Forms (WinForms) Application using C# and VB.Net.
The Multi-Series Line Chart (Graph) will be populated from SQL Server database in Windows Forms (WinForms) Application using C# and VB.Net.
The Multi-Series Line Chart is different from Line Chart as compared to Line Chart which simply displays only one line, the Multi-Series Line Chart displays multiple lines representing the statistical information.
 
 
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 Multi-Series Line Chart (Graph) in Windows Forms Application using C# and VB.Net
 
 

Form Design
The Form consists of a Chart control.
Create Multi-Series Line Chart (Graph) 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 Multi-Series Line Chart from database in Windows Forms Application
Inside the Form Load event of the page, the Multi-Series Line Chart is populated from database.
The SQL Query gets the Country-wise Order distribution for three years. The records from database are populated into a DataTable.
The populated DataTable will contain statistical data for multiple countries in following format. Basically it is the statistical data of Orders of each Country for each Year. Example, Total Orders of Brazil for Year 1996, 1997 and 1998.
A loop is executed over the Countries present in DataTable and one by one series are added to the Chart control.
C#
private void Form1_Load(object sender, EventArgs e)
{
    //Fetch the Statistical data from database.
    string query = "SELECT ShipCountry, DATEPART(Year, OrderDate) [Year], COUNT(OrderId) [Total]";
    query += " FROM Orders WHERE ShipCountry IN ('France', 'Germany', 'Brazil')";
    query += " GROUP BY ShipCountry, DATEPART(Year, OrderDate)";
    DataTable dt = GetData(query);
 
    //Get the DISTINCT Countries.
    List<string> countries = (from p in dt.AsEnumerable()
                              select p.Field<string>("ShipCountry")).Distinct().ToList();
 
    //Remove the Default Series.
    if (Chart1.Series.Count() == 1)
    {
        Chart1.Series.Remove(Chart1.Series[0]);
    }
 
    //Loop through the Countries.
    foreach (string country in countries)
    {
 
        //Get the Year for each Country.
        int[] x = (from p in dt.AsEnumerable()
                   where p.Field<string>("ShipCountry") == country
                   orderby p.Field<int>("Year") ascending
                   select p.Field<int>("Year")).ToArray();
 
        //Get the Total of Orders for each Country.
        int[] y = (from p in dt.AsEnumerable()
                   where p.Field<string>("ShipCountry") == country
                   orderby p.Field<int>("Year") ascending
                   select p.Field<int>("Total")).ToArray();
 
        //Add Series to the Chart.
        Chart1.Series.Add(new Series(country));
        Chart1.Series[country].IsValueShownAsLabel = true;
        Chart1.Series[country].BorderWidth = 3;
        Chart1.Series[country].ChartType = SeriesChartType.Line;
        Chart1.Series[country].Points.DataBindXY(x, y);
    }
 
    Chart1.Legends[0].Enabled = true;
}
 
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 ShipCountry, DATEPART(Year, OrderDate) [Year], COUNT(OrderId) [Total]"
    query += " FROM Orders WHERE ShipCountry IN ('France', 'Germany', 'Brazil')"
    query += " GROUP BY ShipCountry, DATEPART(Year, OrderDate)"
    Dim dt As DataTable = GetData(query)
 
    'Get the DISTINCT Countries.
    Dim countries As List(Of String) = (From p In dt.AsEnumerable() _
                                       Select p.Field(Of String)("ShipCountry")).Distinct().ToList()
 
    'Remove the Default Series.
    If Chart1.Series.Count() = 1 Then
        Chart1.Series.Remove(Chart1.Series(0))
    End If
 
    'Loop through the Countries.
    For Each country As String In countries
 
        'Get the Year for each Country.
        Dim x As Integer() = (From p In dt.AsEnumerable() _
                              Where p.Field(Of String)("ShipCountry") = country _
                              Order By p.Field(Of Integer)("Year") _
                              Select p.Field(Of Integer)("Year")).ToArray()
 
        'Get the Total of Orders for each Country.
        Dim y As Integer() = (From p In dt.AsEnumerable() _
                              Where p.Field(Of String)("ShipCountry") = country _
                              Order By p.Field(Of Integer)("Year") _
                              Select p.Field(Of Integer)("Total")).ToArray()
 
        'Add Series to the Chart.
        Chart1.Series.Add(New Series(country))
        Chart1.Series(country).IsValueShownAsLabel = True
        Chart1.Series(country).BorderWidth = 3
        Chart1.Series(country).ChartType = SeriesChartType.Line
        Chart1.Series(country).Points.DataBindXY(x, y)
    Next
 
    Chart1.Legends(0).Enabled = True
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 Multi-Series Line Chart (Graph) in Windows Forms Application using C# and VB.Net
 
 
Downloads