Dynamic Data Grouping Using MS Reporting Services


I still remember it was a neatly done report that got me my first pay raise. Ever since, I am very passionate about report writing. (Everyone likes a pay raise, right?) In this article, I will guide you step by step how to create a simple report using MS Reporting Services 2005 and host it with a Smart Client application.

So, are you ready to get your share of a pay raise? Why not! Who knows, your neatly done report can just do that.

Prior to this article, I wrote three others that addressed different issues related to the reporting services. However, all of them were targeted towards the intermediate-advanced level audience. From all the feedback I received, one was common: Quite a few of you asked for an article that was specifically geared towards the novice-beginner level.

I assume the reader has the basic understanding of the Visual Studio 2005 IDE and is comfortable with writing code using C#. You don’t have to know the MS Reporting Services to understand this article; but, any pervious experience with report writing would help to put yourself on the fast track.

Note: Although I call this article “101,” my intention is to adopt the applied approach rather then discuss each and every topic associated with reporting services. I touch on most common aspects of report designing with most commonly used controls. I would strongly encourage you to please go through the MSDN documentation for more detailed information.

Roll up Your Sleeves—It’s Reporting Time

Please take a look at Figure 1. How complex is that report? How much time do you think it would take to create such a report? Well, as for complexity, it is a simple report extracted from NorthWind->Products (Sql Server 2000) and lists all the products’ information with summary totals.

Figure 1

About time, obviously, it should not take you hours to do it. About R&D and trial & error time, I leave that to you; dig down deep. The deeper you will explore, the better the treasure you will find.

Here it is, the million-dollar question: How do you start? What is the first step?

Often, it is very easy to find out what the first step should be. Have you seen a house built before the foundation? No! So, have I given you a hint here? You must first develop the Smart Client to host your report.

Step 1: Create a Windows Application Project

Please do the following to create a Windows Application (Smart Client) project:

  • Select File menu -> New -> Project.
  • Choose C# from the Project Types pane.
  • In the Templates pane, choose Windows Application for Visual C# projects.

In the Name box, name the project something unique (I named the attached project code rsWin101) to indicate the application’s purpose. In the Location box, enter the directory in which you want to save your project, or click the Browse button to navigate to it. Once you are done, you will find Form1 added to the project and you can start working on it using Forms Designer.

Please update following properties of Form1:

Form1.Text = "MS Reporting Services 101 with Smart Client"
Form1.Size = 750, 300

Feel free to change any other property of Form1 as per your requirement.

Step 2: Add Report Viewer to the Form

So, what is report viewer? Just As you need a DVD player to play a DVD, the same goes with the reports. You need a report viewer to have the report preview done.

For all those who are brand new to report writing, I would say that report viewer gives life to your reports. It not only previews you the output; further, it also facilitates you to generate the information in the various popular formats (PDF, Excel, and so forth). You also can make a hard copy print of the report while you are viewing the output.

Please perform following actions to setup Report Viewer Control on Form1:

  • Drag ToolBox -> Data -> ReportViewer and drop it on Form1. This step will create a new instance of ReportViewer with the name of reportViewer1. I always wanted to name reportViewer1 to rpvAbraKaDabra; I wont let this chance pass by now. As I picked rpvAbraKaDabra, feel free to pick yours; let those imagination horses run wild!
  • By setting reportViewer1.Dock = Fill, report viewer will fill the entire surface of the form for report display purposes.

After Steps 1 and 2, your project should look like Figure 2.

Figure 2

Step 3: Add DataSet to the Project

Hurray! You are done with the foundation. It’s time to put walls around the foundation; eventually, these walls will hold the doors and windows of your home. DataSet is just that for Report Viewer; it holds and provides the raw data from the data source to be processed and ready to be output on the Smart Client interface.

The following step is required to have DataSet added to project:

  • Select Add -> New Item -> DataSet from Solution Explorer. Change its name from DataSet1 to dsProduct and click the Add button to complete the action.

Add a DataTable to your newly created DataSet. DataTable is essential to load the reporting data; you will use the information from DataSet/DataTable when designing the report.

The following step is required to have DataTable added to DataSet(dsProduct):

  • Double-click on dsProduct from Solution Explorer; it will open the designer view. Right-click on the designer surface and Add -> DataTable. Please click on the header and change the name to dtProductList. Please see Figure 3.

Figure 3

Start adding columns to DataTable(dtProductList). Your designer screen should look like Figure 4. Right-click on dtProductList and select Add -> Column to start adding columns to DataTable.

Figure 4

Please repeat the action for the following columns:

  • ProductName (String)
  • QuantityPerUnit (String)
  • UnitPrice (Double)
  • UnitsInStock (Double)
  • UnitValue (Double): A calculated field based on UnitsInStock * UnitPrice

As you are adding columns, by default it is the string data type. Please go to the properties windows after selecting a column to change it from String to Integer or Double.

Please see Figure 5. Your DataTable should look the same. Also, you can see the properties window to change the data type.

Figure 5

Have you heard of “Typed DataSet“? If not, you have just created a Typed DataSet here. Please consult online help to know more about Typed DataSet.

Step 4: Add a Report to the Project

All right. So far, you created the project, and added Report Viewer and DataSet. Now, it is the time to deal with star of the show! It’s time to create that neat report.

The following steps are required to have Report (rptProductList.rdlc):

  • Select Add -> New Item -> Report from Solution Explorer. Change its name from Report1.rdlc to rptProductList.rdlc and click the Add button to complete the action.

Typically, after the add action is finished your screen should be similar to Figure 6. When a report is added to project, it is ready to use the DataSet for designing.

Figure 6

Whether this is your very first report or you are a reporting junkie like me, you have to deal with the most basic building blocks of report writing, which are: Header, Body, and Footer.

Typically, reports are designed with a specific page size and layout in mind. Your report is Letter size and Portrait layout. You can explore various properties attached to the report layout by right clicking anywhere on the open designer surface and selecting Properties.

It is always advisable to draw a prototype of your report on paper, before you start the design attempt. As you can see in Figure 1, you have the Report Name and Report Date in the header section. The body section has the product list information together with summary totals; the footer carries the Page Numbers.

Start working on the Page Header:

When a new report is added to the project, by default, all you will see in the report designer is the body section. Right-click on the report designer surface anywhere other than the body and select Page Header. This will add a header to the report. Feel free to adjust the height of header and body section. See Figure 7; I have reduced the height of body and increased the height of the header.

Figure 7

While inside the report designer, if you explore the Toolbox, you will see a variety of controls that can be used to design your report. For your example, you will use the TextBox, Line, and Table control. I would encourage you to go through the online documents if you need detailed information for all available controls.

Header Section

Start designing the header. Drag two TextBoxes and drop them on the header section. Texbox can show both static and dynamic data. The Line control is used to separate the header from the body section.

After dropping controls over the report designer surface, you can control the look and feel by changing the associated properties. You will designate one TextBox to the report title and another one to show the current date. You can directly type static text into the TextBox control by selecting it and typing inside.

Please change following properties of Title TextBox:

Value = "Product List"
Color = Purple (you like a purpose, too, for the title, right?)

Please change following properties of Date TextBox:

Value = "Run Data: " & Today
Color = Purple (you like a purpose, too, for the title, right?)

Please note the Value property for the Date TextBox starts with a “=” sign. This is not simple static text; instead, it is an expression. This expression is a result of the string “Run Date” and the VB.NET script keyword Today (to get the current system date).

You can specify desired names to all objects in report; I chose to stay with default name for most of the controls. However, for demo purposes, I did specify “txtTitle” to Title TextBox.

Please refer to Figure 8; your finished design for header should look relatively the same.

Figure 8

More by Author

Must Read