Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

December 15, 2010

Tie a Report to Data from Business Objects

Filed under: C#,Reporting,VB.NET @ 3:49 pm

This prior post provides an overview of reporting in your WinForms or WebForms application. This post details how to tie your report to data defined within your business objects.

See this prior post if you want to tie your report directly to data in the database.

In this example, the report built in this prior post is tied to a list of Customer business objects. The list of customer objects is defined in this prior post and summarized below:

In C#:

Customer class:

public class Customer
{
    public int CustomerId { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string EmailAddress { get; set; }
}

List of customers:

List<Customer> custList = new List<Customer>
                    {new Customer()
                          { CustomerId = 1,
                            FirstName="Bilbo",
                            LastName = "Baggins",
                            EmailAddress = "bb@hob.me"},
                    new Customer()
                          { CustomerId = 2,
                            FirstName="Frodo",
                            LastName = "Baggins",
                            EmailAddress = "fb@hob.me"},
                    new Customer()
                          { CustomerId = 3,
                            FirstName="Samwise",
                            LastName = "Gamgee",
                            EmailAddress = "sg@hob.me"},
                    new Customer()
                          { CustomerId = 4,
                            FirstName="Rosie",
                            LastName = "Cotton",
                            EmailAddress = "
rc@hob.me"}};

In VB:

Customer class:

Public Class Customer
Public Property CustomerId As Integer
    Public Property FirstName() As String
    Public Property LastName() As String
    Public Property EmailAddress() As String
End Class

List of customers:

Dim custList As New List(Of Customer) From
            {New Customer() With
                  {.CustomerId = 1,
                    .FirstName = "Bilbo",
                    .LastName = "Baggins",
                    .EmailAddress = "bb@hob.me"},
            New Customer() With
                  {.CustomerId = 2,
                    .FirstName = "Frodo",
                    .LastName = "Baggins",
                    .EmailAddress = "fb@hob.me"},
            New Customer() With
                  {.CustomerId = 3,
                    .FirstName = "Samwise",
                    .LastName = "Gamgee",
                    .EmailAddress = "sg@hob.me"},
            New Customer() With
                  {.CustomerId = 4,
                    .FirstName = "Rosie",
                    .LastName = "Cotton",
                    .EmailAddress =
rc@hob.me}}

To Build a Data Source for a Report:

1) Display the report designer by double-clicking on the .rdlc file in the Solution Explorer.

2) Display the Data Sources Window (Data | Show Data Sources).

3) Click on the Add New Data Source button in the Data Sources Window toolbar.

4) Select to get the data from an Object and click Next.

image

5) Select the business object that defines the fields for the report and click Finish.

image

Your data source is then created and shown in the Data Sources Window.

To Build a Dataset for a Report:

A report is tied to a data source through a dataset. A dataset defines the data fields available to the report. You define a dataset based on a data source (created in the steps above).

This term "dataset" should not be confused with an ADO.NET dataset. This is not the same thing. In this context, a "dataset" is simply a set of data that can be used on a report.

1) Display the report in the designer by double-clicking on the .rdlc file in the Solution Explorer.

2) Display the Report Data Window (View | Report Data).

NOTE: If you do not see the Report Data menu option, ensure that the report designer is the active window.

3) Select New | Dataset from the Report Data window toolbar.

This creates a dataset that is accessible from the report.

4) Enter the name of the dataset, select the data source to use as the source of data for the dataset (defined in the prior set of steps), and select the data object within the data source to tie to the report.

The fields available to the report are then shown:

image

NOTE: By my programming convention, I name the report’s dataset "XXXDataSource" so as not to be confused with any typed dataset in the project.

The fields also appear in the Report Data window:

image

NOTE: To view the Report Data window, ensure that a report is the active Window in design mode. Then select View | Report Data.

To Display Data from the Data Source on the Report:

1) Display the report in the designer by double-clicking on the .rdlc file in the Solution Explorer.

2) Hover over a cell in the data row, click on the field list (circled in red in the image), and select the field to display in the cell.

image

3) Repeat for each cell in the table.

4) To add fields from the dataset that are not already shown in the table, drag the field from the Report Data Window and drop it in the table.

5) Write the code to populate the data source.

When you tie a report directly to a database, the data is automatically retrieved when you run the report. When you tie a report to a set of business objects, you need to populate the underlying data source.

In C#:

private void ReportWin_Load(object sender, EventArgs e)
    this.customerBindingSource.DataSource = Customers.RetrieveList();
    this.reportViewer1.RefreshReport();
End Sub

In VB:

Private Sub ReportWin_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
    Me.CustomerBindingSource.DataSource = Customers.RetrieveList
    Me.ReportViewer1.RefreshReport()
End Sub

This code assumes that a static/shared method in the Customers class called RetrieveList retrieves the list shown at the top of this post.

Use this technique any time you want your report to pull its data from your business objects.

Enjoy!

EDIT 2/18/11: Added a note with details on viewing the Report Data window.

2 Comments

  1.   Jesus - — February 18, 2011 @ 10:40 am    Reply

    Hi.

    Thank you for this report.

    Only a question; I’m getting crazy!! I can not find report data window. I have my rldc report in the active window and in disegn mode, and is impossible to find the report data window to define the necessary data sets.

    Can you help me? PLEASE!! I have spende hours and is not possible for me.

    I can send you an screen snapshot if you want. It doesn’t appear.

    Thank you in advance.

  2.   DeborahK — February 18, 2011 @ 3:11 pm    Reply

    Hi Jesus –

    I have Visual Studio 2010 Ultimate. On my system, the Report Data Window is accessible from View | Report Data when a report is active in the designer.

    Hope this helps.

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2014 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs