Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

RDLC Report & ReportViewer Control in C#

1. RDLC Reports Overview

In this RDLC Reports tutorial, we will explore how to generate the RDLC Reports using the Microsoft supplied report template (.RDLC file). We get the data from the ‘Titles’ table of the Microsoft supplied ‘Pubs’ database. In this example, the ReportViewer Control acts as a presentation UI.  To generate the report on the database table, we will use following things:

  1. ‘DataSource’ that supplies the data from the database.
  2. ‘Report Template’ that defines the visible elements and its location. [.RDLC file]
  3. ‘ReportViewer Control’ supplied by the dot net framework.
  4. ‘C-Sharp Form’ that houses the ReportViewer control in it.

The interaction between the different aspects in generating the report is shown below:

RDLC Report Overview

RDLC Report Overview

We define the report elements in a template called ‘Report Template’. The report template holds the ‘.RDLC’ file extension. This includes database fields, text labels, Images, etc. The Report Template defines what we want to display, and in what format & order we want to display it. Once the report template is ready, one can assign it to a ‘ReportViewer Control’ which we host in a container say, for example, a Dot Net Form.

2. About the RDLC Report Example

The finished report in the design view looks like the below screen:

Designing the RDLC Reports - Page Header & Footer, Report Header, Footer & Details

Designing the RDLC Reports – Page Header & Footer, Report Header, Footer & Details

From the designer it is obvious that we will display the data for the report in a ‘Table Control’. In the Page Header of the report, we will display an image. Similarly, in the Page Footer we will display the report title and the page number of the report. We will explore these in detail when we progress through this example.

The below screenshot shows how the report will look like when we run it:

Our RDLC Report Example - How it looks at runtime

Our RDLC Report Example – How it looks at runtime?

Note, in the data part (number 4) of the report the rows are highlighted when the price is 20 or more. Let us build this report and as you guess there is no piece of code that we need to write here. The above screenshot is a C# Form with a ReportViewer Control in it. When we want to display the report, we can call this Windows Form from different Dialog or Menu Item. OK, we can now begin with creating a new Visual C# Windows application.

3. Defining DataSource for RDLC Report

After creating the project we have to click on the Menu Option ‘Add New Data Source…’ as shown in the below screen. This will open a wizard and using that we can connect to the database for defining the DataSource to the project. With a valid connection to a database one can bring the data to the project by picking a table, views, stored procedure, etc.

Add DataSource to the Project

Add DataSource to the Project

In this example, we will connect to SQL server database on the local machine to use ‘Titles’ table from the ‘Pubs’ database. The ‘Show Data Sources’ menu item in the ‘Data’ menu brings the Data Sources window which we can dock to any side of our visual studio IDE.


Video 1: Define DataSource For SQL Server’s Pubs Database


4. Presenting RDLC Report Data in Table Control

We set data source for our project in the previous section. The next step is adding the report template to the project. We can add the report template to the project using the ‘Add new item’ context menu option and from the template list we must select the ‘Report’ item. Once the report design view is displayed, we add the table control from the toolbox to the report. The below picture shows the report design view:

RDLC Report With Table Control in it

RDLC Report With Table Control in it

The Table Control shows three sections in it. The ‘Header’ is useful to display the column titles. Data retrieved from database flows from start-to-end in the ‘Detail’ section of Table Control. Once the data portion is displayed in the detail part of the table control, in the ‘Footer‘ we can place some summary information like total, average, counts, etc. In our example we will place a static text called ‘End of Report’.

The Table Control shows three columns by default. However, one can add more columns by clicking the ‘Insert Column to Left’ or ‘Insert Column to Right’ options based on the need. We can drag a table field from the Data Source Window and drop it to the Detail Cell of the Table Control and doing so will add a relevant column title in the Header Cell. In our example, we pick four columns from the Titles table and drop it to the detail section of the Table Control which sitting in the RDLC Report.


Video 2: Position the required database fields in the report


5. Assigning RDLC Report to ReportViewer Control

The report template is ready. Next, we need to assign our RDLC Report to the ReportViewer Control. The ReportViewer Control will query the database and shows the report using the Report Template (RDLC File). The below screen shows a ReportViewer control in the form:

Setup ReportViewer Control to Present RDLC Report Content

Setup ReportViewer Control to Present RDLC Report Content

After assigning the ‘.RDLC’ file to the ReportViewer Control, we dock ReportViewer to the entire form so that the user will get a better look at the report. That is all we need to show a report. Assigning the report to the report view control and conducting a test run is shown in the below video with explanation:


Video 3: Assigning the Report Template to ReportViewer Control & Running the report


6. Adding Page Header and Page Footer

6.1 About Page Header & Footer

Section four of this article talked about Report Header and report footer. Now we will add the ‘Page Header’ and ‘Page Footer’ to the report. Before we add the Page Header and Footer, we should understand how it would differ from the Report Header and Footer. The depiction below assumes a report spawns for three pages:

Page Header and Page Footer of RDLC Report

Page Header and Page Footer of RDLC Report

From above picture we can note that Report Header starts before starting the report details section. Report Footer starts after the detail section ends. Also, know that the details section takes responsibility of displaying the database records and computed fields. The RDPC Report will display page Header and Page Footer on every page. OK! Now if I ask you where I should keep the page numbers when I am designing the report, you will promptly reply, “Page Footer”.

6.2 Adding Page Header & Footer to RDLC Report

To add Page header and page footer to the report template, we should click the square-shaped area in the right corner of the report (focused in the below picture) and then right click to bring the short-cut menu. From this menu, we can choose the Page Header and Page Footer.

Adding Page Header and Page Footer to RDLC Reports

Adding Page Header and Page Footer to RDLC Reports

Once Page Header and Page Footer are added to the report template, we add an image control to the report template’s Page Header section. We will populate this image using the embedded image. One can add an embedded image from report menu as shown below:

Add Image to an RDLC Report

Add Image to an RDLC Report

After embedding the image into the RDLC Report, we set the following properties to the image control added in the Page header section of the report:

Embedded Image Properties

Embedded Image Properties

Similarly, we add two text box controls to the Page Footer section of the report. These text boxes are used to display report name and page numbers. To get report name and page numbers, the control’s value property is set with an expression that computes Page Number, gets report name and finally displays it. You can see setting the Page Header and Footer from the below-shown video:


Video 4: Setting the Page Header and Page Footer


7. Highlighting Costly Books Based on Price

To highlight the costly books, we select the details row and set the background and color property through an Report Expression. These expressions are below:

The Report preview in Section 2 shows the highlighted rows. We can build the above-shown expression  using the Expression Builder. Highlighting the costlier books by building the expression is shown in the below-given video.


Video 5: Building the expression from DB fields to highlight a row


Source Code : Download RDLC Report Example From Google Drive

Categories: C#

Tags: , , , , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.