Binding RDLC Report to Windows Form
Advanced
20 min read
Lesson 4 of 5
What You Need
- ReportViewer control - The visual control that shows the report
- RDLC report file - The report you designed
- Dataset with data - The data to fill the report
- Data source binding - Connecting the data to the report
Step 1 — Add ReportViewer Control
// Install NuGet Package
Install-Package Microsoft.ReportingServices.ReportViewerControl.WinForms
// Add to Toolbox
1. Right-click Toolbox → Choose Items
2. Select Microsoft Report Viewer
Step 2 — Configure the Form
// Form Designer
public partial class ReportForm : Form
{
public ReportForm()
{
InitializeComponent();
}
}
Step 3 — Load and Bind the Report
private void ReportForm_Load(object sender, EventArgs e)
{
// Create dataset
DataSet dataSet = new DataSet();
DataTable productsTable = GetProductData();
dataSet.Tables.Add(productsTable);
// Configure report viewer
reportViewer1.Reset();
reportViewer1.ProcessingMode = ProcessingMode.Local;
// Load report
reportViewer1.LocalReport.ReportPath = "ProductReport.rdlc";
// Set data source
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(
new ReportDataSource("ProductDataSet", dataSet.Tables[0]));
// Refresh report
reportViewer1.RefreshReport();
}
Step 4 — Get Data from Database
private DataTable GetProductData()
{
DataTable table = new DataTable();
string connectionString = "Server=localhost;Database=MyApp;Trusted_Connection=True;";
string query = "SELECT ProductId, Name, Price, Description, Category FROM Products";
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlDataAdapter adapter = new SqlDataAdapter(query, conn))
{
adapter.Fill(table);
}
}
return table;
}
Step 5 — Complete Code Example
using Microsoft.Reporting.WinForms;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
public partial class ReportForm : Form
{
public ReportForm()
{
InitializeComponent();
}
private void ReportForm_Load(object sender, EventArgs e)
{
LoadReport();
}
private void LoadReport()
{
try
{
DataTable data = GetProductData();
reportViewer1.Reset();
reportViewer1.ProcessingMode = ProcessingMode.Local;
reportViewer1.LocalReport.ReportPath = "ProductReport.rdlc";
ReportDataSource dataSource = new ReportDataSource("ProductDataSet", data);
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(dataSource);
reportViewer1.SetDisplayMode(DisplayMode.PrintLayout);
reportViewer1.ZoomMode = ZoomMode.Percent;
reportViewer1.ZoomPercent = 100;
reportViewer1.RefreshReport();
}
catch (Exception ex)
{
MessageBox.Show($"Error loading report: {ex.Message}", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private DataTable GetProductData()
{
DataTable table = new DataTable();
string connectionString = "Server=localhost;Database=MyApp;Trusted_Connection=True;";
string query = "SELECT ProductId, Name, Price, Description, Category FROM Products";
using (SqlConnection conn = new SqlConnection(connectionString))
using (SqlDataAdapter adapter = new SqlDataAdapter(query, conn))
{
adapter.Fill(table);
}
return table;
}
}
Key Takeaway
Binding reports to Windows Forms is straightforward using the ReportViewer control and local processing.