In this article I will explain a simple tutorial with an example and sample code to create RDLC Report in Windows Forms (WinForms) application using C# and VB.Net.
The RDLC Report in in Windows Forms (WinForms) application will be populated using Typed DataSet.
Database
Here I am making use of Microsoft’s Northwind Database. The download and install instructions are provided in the following article.
1. Add Typed DataSet to the Project
Since I am using disconnected RDLC Reports we will make use of Typed DataSet to populate the RDLC Reports with data from database.
2. Adding DataTable to the Typed DataSet
Our next step would be to add a DataTable to the Type DataSet.
3. Adding Columns or fields to DataTable
In the DataTable we need to specify the column names that we want to display in the RDLC Report.
Note: The Column Names of the DataTable must exactly match with the actual Database Table column names.
By default all the columns are of String Data Type but you can also change the data type as per your need.
4. Adding the RDLC Report
Using the Add New Item option in Visual Studio you need to add new RDLC Report. I am making use of Report Wizard so that it make easier to configure the Report.
5. Choose the DataSet
Now we need to choose the DataSet that will act as the DataSource for the RDLC Report. Thus we need to select the Customers DataSet that we have created earlier.
6. Choose the Fields to be displayed in the RDLC Report
Next we need to choose the fields that we need to display, we need to simply drag and drop each fields into the Values Box as shown in the screenshot below.
7. Choose the Layout
The next dialog will ask us to choose the layout, we can simply skip it as of now as this is a simple Report with no calculations involved.
8. Choose the Style
Finally we need to choose the style, i.e. color and theme of the Report.
Once you press Finish button on the above step, the Report is ready and is displayed in the Visual Studio as shown below.
9. Adding Report Viewer to the Form
In order to display the Report we will need to add ReportViewer control to the Form from the Toolbox.
Once you add the ReportViewer control to the Form, your Form must look as below. You will need to click on the small arrow present on the top right corner of the Report Viewer and choose the RDLC Report as shown below.
10. Populating the RDLC Report from Database
Below is the code to populate the RDLC Report from database. The Customers DataSet is populated with records from the Customers Table and is set as ReportSource to the Report.
C#
Namespaces
using System.Data;
using System.Data.SqlClient;
using Microsoft.Reporting.WinForms;
Code
private void Form1_Load(object sender, EventArgs e)
{
Customers dsCustomers = GetData();
ReportDataSource datasource = new ReportDataSource("Customers", dsCustomers.Tables[0]);
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(datasource);
this.reportViewer1.RefreshReport();
}
private Customers GetData()
{
string constr = @"Data Source=.\Sql2005;Initial Catalog=Northwind;Integrated Security = true";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 20 * FROM customers"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (Customers dsCustomers = new Customers())
{
sda.Fill(dsCustomers, "DataTable1");
return dsCustomers;
}
}
}
}
}
VB.Net
Namespaces
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.Reporting.WinForms
Code
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim dsCustomers As Customers = GetData()
Dim datasource As New ReportDataSource("Customers", dsCustomers.Tables(0))
Me.ReportViewer1.LocalReport.DataSources.Clear()
Me.ReportViewer1.LocalReport.DataSources.Add(datasource)
Me.ReportViewer1.RefreshReport()
End Sub
Private Function GetData() As Customers
Dim constr As String = "Data Source=.\Sql2005;Initial Catalog=Northwind;Integrated Security = true"
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT TOP 20 * FROM customers")
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dsCustomers As New Customers()
sda.Fill(dsCustomers, "DataTable1")
Return dsCustomers
End Using
End Using
End Using
End Using
End Function
Source:
http://www.aspsnippets.com/Articles/RDLC-Report-in-Windows-Forms-WinForms-Application-using-C-and-VBNet.aspx