Adding a local report in WinForms.

Alex van Buitenen

Intro.

This is not a tutorial nor a complete quick reference.
This document is just my personal memo block for C#.

Adding a local report in WinForms.

WinForms has a ReportViewer control. This control can be used to view either: This note is about a local report. The Report controls have a somewhat different look and feel as the other Windows Forms components. Therefore it can be toublesome to start with it. This note guides you in some simple steps thru the process of creating a very simple report. That should give you enough feeling to get started with the Report components.

Add a report.

Rightclick Project, Add, New Item.
Choose Reporting, Report

Insert a page header.

The .rdlc file is created and opened.
Right click the report, Insert, Page Header

Adjust background color.

Adjust background color.

Add parameter to report.

Open Report data properties window (View, Report data (Available when .rdlc file is opened))

Add parameter "HeaderText"

Add textbox to header and fill with parameter value.

Drag Textbox from Toolbox on header.

Click on Textbox. The propertiew window changes from "Textbox" to "Selected text".

Notice that the properties window does not yet conatin a value member.

Righclick the "selected text" and choos "Create Placeholder".

Press fx for Value and pick parameter "Headertext".

Make data available.

Add a person to the project:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestReport
{
 public class Person
  {
   public string Name { get; set; }
   public int Age { get; set; }

   public Person(string name, int age)
   {
    Name = name;
    Age = age;
   }
  }
}

Drag a table (tablix) to the body and define the Datasource.

Make sure the projects gets compiled, otherwise datasource "Person" will not be available.
Drag Table from ToolBox on body (Dit wordt een "Tablix")
Visual Studio lets you choose a datasource immediately.

Choose the columns to display.

Add a Form with a ReportViewer.

Add a Form to the project.
Add a ReportViewer to the Form.

Connect the ReportViewer to the Report.

Choose the report in ReportViewer by clicking on the little arrow in the upper rights corner. This results in the porperty "LocalReport.ReportEmbeddedResouce" to be filled.

Fill the parameter and the datasource.

Fill the reports parameter and datasource, for instance:

using Microsoft.Reporting.WinForms;

private void reportViewer1_Load(object sender, EventArgs e)
{
this.reportViewer1.LocalReport.SetParameters(new ReportParameter("HeaderText", "Persons"));
this.reportViewer1.LocalReport.DataSources[0].Value = GetPersons();
}

private List<Person> GetPersons()
{
List<Person> persons = new List<Person>();
Person piet = new Person("Piet", 30);
Person jan = new Person("Jan", 25);
persons.Add(piet);
persons.Add(jan);
return persons;
}

View the result.