Creating chart with Crystal Report application.

Crystal Report also supports the creation of charts. Now we are going to see how we can display a chart showing the percentages of a particular product ordered by the each customer.
Add new crystal report in our solution and do as the following steps.

1. Add the following tables to the report
Customers
Order Details
Orders
Products
2. Choose the following fields to display:
Customers.CompanyName
Products.ProductName
Order Details.Quantity
3. Group the report by Customers.CompanyName.
4. For the Summaries section, ensure that only Sum of Order Details.Quantity is present.
5. In the Chart dialog, check the Pie Chart option.

Now, add a new Parameter Field to the report and name the parameter Product_Name. Click the Select Expert button and bind the parameter to the Products.ProductName field of the report.

Finally, add the following controls to Form1:
- Label
- ComboBox
- Button
Write the code as following to view chart,

private void Form1_Load(object sender, EventArgs e)
{
string connStr = "Data Source=.\\SQLEXPRESS;Initial Catalog=myDB;Integrated Security=True";
string sql = "SELECT CustomerID FROM Customers";
SqlConnection conn = new SqlConnection(connStr);
SqlCommand comm = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader reader = comm.ExecuteReader;
while (reader.Read)
{
comboBox1.Items.Add(reader(0));
}
conn.Close();
sql = "SELECT ProductName from Products";
comm.CommandText = sql;
conn.Open();
reader = comm.ExecuteReader;
while (reader.Read)
{
ComboBox2.Items.Add(reader(0));
}
conn.Close();
}


In button click event,

private void btnViewReport_Click(object sender, System.EventArgs e)
{
CrystalReport2 report = new CrystalReport2();
report.SetParameterValue("Product_Name", ComboBox2.Text);
{
Form2.CrystalReportViewer1.ReportSource = report;
Form2.ShowDialog();
}
}


That’s it… Now you can test with running the application.

...S.VinothkumaR.

No comments: