How to create grouping in crystal report?


In this article I am going to explain about crystal report grouping . This concept is used in various projects for developer. Follow this below steps to create grouping in crystal report easily.

Description


For example I have store lots of employee information in the employee table. There are two categories of employee in the database table.
1) Admin category
2) Non Admin category

Using this category I have grouping in the crystal report.

Table structute



create table emp(eno int,empname varchar(50),desig varchar(50),sal decimal(15,2),Category varchar(15))

Default page : Client side


In this page I have collected employee details like below

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Crystal Report Grouping</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="0" cellspacing="0" align="center" width="700">
<tr>
<td height="40" colspan="2">
<b>New Employee Registration</b>
</td>
</tr>
<tr>
<td height="40" colspan="2" align="center">
<asp:Label ID="Label1" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td height="30" align="left" width="50%">
Employee No.
</td>
<td height="30">
<asp:TextBox ID="txteno" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td height="30" align="left" width="50%">
Name
</td>
<td height="30">
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td height="30" align="left">
Designation
</td>
<td height="30">
<asp:TextBox ID="txtdesig" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td height="30" align="left">
Salary
</td>
<td height="30">
<asp:TextBox ID="txtsal" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td height="30" align="left">
Category
</td>
<td height="30">
<asp:DropDownList ID="ddlAdmin" runat="server">
<asp:ListItem>Admin</asp:ListItem>
<asp:ListItem>Non Admin</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td height="40" colspan="2" align="center">
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

Design side look like this
images

Default page : Server side


In this page I have inserted that collected data into the database table

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());
SqlCommand sqlcmd = new SqlCommand();

protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "";
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
sqlcon.Open();
sqlcmd = new SqlCommand("insert into emp(eno,empname,desig,sal,Category) values('" +txteno.Text + "','" +txtname.Text + "','" + txtdesig.Text + "','" + txtsal.Text + "','" + ddlAdmin.SelectedItem.Text + "')", sqlcon);
sqlcmd.ExecuteNonQuery();
Label1.Text = "Record insert successfully";
clear();
}
catch (Exception ex)
{

}
finally
{
sqlcon.Close();
}
}
void clear()
{
foreach (Control c in form1.Controls) //This loop takes all controls from the form1
{
//Clear all textbox values
if (c is TextBox)
((TextBox)c).Text = "";

//clear all check boxes
if (c is CheckBox)
((CheckBox)c).Checked = false;

//Clear all radio buttons
if (c is RadioButton)
((RadioButton)c).Checked = false;
}
}
}

Create Dataset with fields
Now right click on the project name add DataSet1.xsd into the App_Code folder and right click on the designer to add new datatable with the table fields like below
images

Create Crystal Report
Now right click on the Project name again and add new crystal report in the project name it as CrystalReport.rpt
Then right click on the Field Explorer and choose database expert choose newly created DataSet1 -> DataTable1 and click ok button to add that DataSet1 Fields in the crystal report designer.
images

Design Crystal report
Design your crystal report with the Field explorer fields and follow below steps to create grouping in crystal report
1) Right click on the crystal report choose insert Grouping
images

2) In the next screen select Groping field means grouping based on which field.
images

3) Design your crystal report like below
images

4) Create sum of salary based on the group
images

5) Next screen choose which field you want summaries
images


Default2 page : client side


I have placed crystal report viewer in this page to show crystal report

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<%@ Register Assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
Namespace="CrystalDecisions.Web" TagPrefix="CR" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Crystal Report Grouping</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<cr:crystalreportviewer id="CrystalReportViewer1" runat="server" autodatabind="true" />
</div>
</form>
</body>
</html>


Default2 page : Server side



using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Web;
using CrystalDecisions.ReportSource;

public partial class Default2 : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ToString());
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
string query;
try
{
//Open sql connection
sqlcon.Open();
//Assign query
query = "select * from emp";
sqlcmd = new SqlCommand(query, sqlcon);
da = new SqlDataAdapter(sqlcmd);
dt.Clear();
//Fill data from sql data adapter
da.Fill(dt);
if (dt.Rows.Count > 0)
{
ReportDocument RptDoc = new ReportDocument();
//Load Crystal report
RptDoc.Load(Server.MapPath("~/CrystalReport.rpt"));
//Set data to that report document
RptDoc.SetDataSource(dt);
//Assign crystal report to the crystal report viewer
CrystalReportViewer1.ReportSource = RptDoc;
CrystalReportViewer1.DataBind();
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
sqlcon.Close();
}
}
}


Output
After followed above steps then run the website to see output look like this
images

Source Code Detail:
Here with I have attached source code of Crystal report grouping download it and try to know about crystal report grouping.
Front End : ASP.NET
Code Behind: C#

Conclusion:
I hope this article help to know crystal report grouping process.


Attachments

  • Source_code (43690-25116-CrystalReportGrouping.rar)
  • Comments

    Author: messyleon12 Sep 2012 Member Level: Bronze   Points : 1

    It depends on how you bind your report to the viewer. If the report is embedded you will have direct access to sections to enable suppress.
    report.Section1.SectionFormat.EnableSuppress = true;
    If you are dynamically loading the report then you have to access the sections through the ReportDefinition
    http://www.dapfor.com/en/net-suite/net-grid/tutorial/data-grouping



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: