Hi hsku6482,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<table>
<tr>
<td>Country 1:</td>
<td>
<asp:DropDownList ID="ddlCountries1" runat="server" AutoPostBack="true" DataSourceID="SqlDataSource1"
DataTextField="ShipCountry" DataValueField="ShipCountry" AppendDataBoundItems="true">
<asp:ListItem Text="Select Country" Value="" />
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConStr %>"
SelectCommand="SELECT DISTINCT ShipCountry FROM Orders ORDER BY ShipCountry"></asp:SqlDataSource>
</td>
<td>Country 2:</td>
<td>
<asp:DropDownList ID="ddlCountries2" runat="server" AutoPostBack="true" DataSourceID="SqlDataSource2"
DataTextField="ShipCountry" DataValueField="ShipCountry" AppendDataBoundItems="true">
<asp:ListItem Text="Select Country" Value="" />
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConStr %>"
SelectCommand="SELECT DISTINCT ShipCountry FROM Orders ORDER BY ShipCountry"></asp:SqlDataSource>
</td>
</tr>
</table>
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false" DataSourceID="GridDataSource">
<Columns>
<asp:BoundField DataField="ShipCountry" HeaderText="Country" />
<asp:BoundField DataField="Year" HeaderText="Year" />
<asp:BoundField DataField="Total" HeaderText="Total" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="GridDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConStr %>"
SelectCommand="SELECT ShipCountry, DATEPART(Year, OrderDate) [Year], COUNT(OrderId) [Total] FROM Orders
WHERE ShipCountry = @Country1 OR ShipCountry = @Country2 GROUP BY ShipCountry, DATEPART(Year, OrderDate)">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCountries1" Name="Country1" PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="ddlCountries2" Name="Country2" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<asp:Button Text="Generate Chart" runat="server" OnClick="OnGenerate" />
<hr />
<asp:Chart ID="Chart1" runat="server" Height="300px" Width="400px">
<Titles>
<asp:Title ShadowOffset="3" Name="Items" />
</Titles>
<Legends>
<asp:Legend Alignment="Center" Docking="Bottom" IsTextAutoFit="False" Name="Default"
LegendStyle="Row" />
</Legends>
<ChartAreas>
<asp:ChartArea Name="ChartArea1" BorderWidth="0" />
</ChartAreas>
</asp:Chart>
Namespaces
C#
using System.Data;
using System.Web.UI.DataVisualization.Charting;
VB.Net
Imports System.Data
Imports System.Web.UI.DataVisualization.Charting
Code
C#
protected void OnGenerate(object sender, EventArgs e)
{
DataTable dtOrder = new DataTable("dtOrders");
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
dtOrder.Columns.Add(cell.Text.Trim());
}
foreach (GridViewRow row in GridView1.Rows)
{
dtOrder.Rows.Add();
for (int i = 0; i < row.Cells.Count; i++)
{
dtOrder.Rows[row.RowIndex][i] = row.Cells[i].Text.Trim();
}
}
List<string> countries = (from p in dtOrder.AsEnumerable()
select p.Field<string>("Country")).Distinct().ToList();
if (Chart1.Series.Count() == 1)
{
Chart1.Series.Remove(Chart1.Series[0]);
}
foreach (string country in countries)
{
int[] x = (from p in dtOrder.AsEnumerable()
where p.Field<string>("Country") == country
orderby p.Field<string>("Year")
select Convert.ToInt32(p.Field<string>("Year"))).ToArray();
decimal[] y = (from p in dtOrder.AsEnumerable()
where p.Field<string>("Country") == country
orderby p.Field<string>("Year")
select Convert.ToDecimal(p.Field<string>("Total"))).ToArray();
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;
}
VB.Net
Protected Sub OnGenerate(ByVal sender As Object, ByVal e As EventArgs)
Dim dtOrder As DataTable = New DataTable("dtOrders")
For Each cell As TableCell In GridView1.HeaderRow.Cells
dtOrder.Columns.Add(cell.Text.Trim())
Next
For Each row As GridViewRow In GridView1.Rows
dtOrder.Rows.Add()
For i As Integer = 0 To row.Cells.Count - 1
dtOrder.Rows(row.RowIndex)(i) = row.Cells(i).Text.Trim()
Next
Next
Dim countries As List(Of String) = (From p In dtOrder.AsEnumerable()
Select p.Field(Of String)("Country")).Distinct().ToList()
If Chart1.Series.Count() = 1 Then
Chart1.Series.Remove(Chart1.Series(0))
End If
For Each country As String In countries
Dim x As Integer() = (From p In dtOrder.AsEnumerable()
Where p.Field(Of String)("Country") = country
Order By p.Field(Of String)("Year")
Select Convert.ToInt32(p.Field(Of String)("Year"))).ToArray()
Dim y As Decimal() = (From p In dtOrder.AsEnumerable()
Where p.Field(Of String)("Country") = country
Order By p.Field(Of String)("Year")
Select Convert.ToDecimal(p.Field(Of String)("Total"))).ToArray()
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
Screenshot