Thursday, 1 December 2011

C# Crystal Reports Summary Field

The following C# - Crystal Reports section describes how to add a summary field in the Crystal Reports .
All C# Crystal Reports Tutorial in this website is based on the following database - crystaldb. So before you begin this section , please take a look at the database structure of crystaldb - Click Here C# crystaldb
If you are new to Crystal Reports and do not know how to create Crystal Reports from C# , please take a look at the section step by step tutorial for creating a Crystal Reports from C#.
This section is the continuation of the previous tutorial C# Crystal Reports Formula Field . So before we start this tutorial , take a look at the previous tutorial C# Crystal Reports Formula Field .
Hope you already gone through the previous section C# Crystal Reports Formula Field. Here in this section we are calculating the grand total of the Formula Field - Total . The Total field is a Formula field, the result of qty X price .
In the Crystal Reports designer view window, right click on the Report Footer , just below the Total field and select Insert -> Summary .
csharp-crystal-report-summay-new
Then you will get a screen , select the Total from the combo box and select Sum from next Combo Box , and summary location Grand Total (Report Footer) . Click Ok button
csharp-crystal-report-summary-insert
Now you can see @Total is just below the Total field in the report Footer.
csharp-crystal-report-summary-total.GIF
Now the designing part is over and the next step is to call the Crystal Reports in C# and view it in Crystal Reports Viewer control .
Select the default form (Form1.cs) you created in C# and drag a button and a CrystalReportViewer control to your form .
You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code.
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;



using System;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ReportDocument cryRpt = new ReportDocument();
            cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt");
            crystalReportViewer1.ReportSource = cryRpt;
            crystalReportViewer1.Refresh();
        }
    }
}

cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt");
The Crystal Reports file path in your C# project files location, there you can see CrystalReport1.rpt . So give the full path name of Crystal Reports file like c:\projects\crystalreports\CrystalReport1.rpt

When you run this program your screen will look like the following picture.

csharp-crystal-report-summary-result
 
Post a Comment