In this Article, We will see How to use the Chart control in Windows Form Application. As we know that Charts and Graphs makes data easier to understand and interpret. Chart is used to present the data in visual form.
Let's Begin:
1. Open Visual Studio (I am using Visual Studio 2012) and Create new Windows Form Application (.NET Framework 4).
2. Drop Chart control from Toolbox present in the Data tab.
Let's Begin:
1. Open Visual Studio (I am using Visual Studio 2012) and Create new Windows Form Application (.NET Framework 4).
2. Drop Chart control from Toolbox present in the Data tab.
3. Go to Chart properties -> Click on Series.
Change the Name of Series. Here, I set Name as Salary. We can also change the ChartType as well as appearance from the Series Collection Editor.
Go to Form1.cs Code and add these line of code:
using System;
using
System.Windows.Forms;
namespace DemoChart
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
fillChart();
}
//fillChart method
private void fillChart()
{
//AddXY value in chart1 in series named as Salary
chart1.Series["Salary"].Points.AddXY("Ajay", "10000");
chart1.Series["Salary"].Points.AddXY("Ramesh", "8000");
chart1.Series["Salary"].Points.AddXY("Ankit", "7000");
chart1.Series["Salary"].Points.AddXY("Gurmeet", "10000");
chart1.Series["Salary"].Points.AddXY("Suresh", "8500");
//chart title
chart1.Titles.Add("Salary
Chart");
}
}
}
|
Displaying Data in Chart control from the Database in Windows Form Application:
1. Create a Database (named as Sample). Add a Table tbl_EmpSalary. The following is the table schema for creating tbl_EmpSalary.
Follow above steps. Add these lines of Code in Form1.cs Code:
using System;
using
System.Windows.Forms;
using System.Data;
using
System.Data.SqlClient;
namespace DemoChart
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
fillChart();
}
//fillChart method
private void fillChart()
{
SqlConnection con =
new SqlConnection("Data Source=.;Initial Catalog=Sample;Integrated
Security=true;");
DataSet ds = new DataSet();
con.Open();
SqlDataAdapter adapt
= new SqlDataAdapter("Select Name,Salary from tbl_EmpSalary", con);
adapt.Fill(ds);
chart1.DataSource = ds;
//set the member of the chart data source used to data
bind to the X-values of the series
chart1.Series["Salary"].XValueMember = "Name";
//set the member columns of the chart data source used
to data bind to the X-values of the series
chart1.Series["Salary"].YValueMembers = "Salary";
chart1.Titles.Add("Salary
Chart");
con.Close();
}
}
}
|
Hope you like it. Thanks.
[Download Source Code via Google Drive]
good explaination
ReplyDeleteit would be nice if you could show some basic game development (like pac man) using c#
Nice exaplanation
ReplyDeleteGood
ReplyDelete