Print RDLC Report in Windows Application using C# and VB.Net

Last Reply 8 months ago By pandeyism

Posted 8 months ago

Hello there,

hows u all?

I am beginner in C# I want to know some more extra techniques.

I have created an windows form application and I want to print the invoice using RDLC reporting method.

i want to print the invoice in A5 Paper Size. 

Thank you I hope I will get positive response from all of u.

You are viewing reply posted by: pandeyism 8 months ago.
Posted 8 months ago Modified on 8 months ago

Hi Prasunjeet,

Please refer below smaple.

For populating RDLC Report in Windows Application refer below article.

RDLC Report in Windows Forms (WinForms) Application using C# and VB.Net

Namespaces

C#

using System.Data.SqlClient;
using Microsoft.Reporting.WinForms;
using System.Drawing.Printing;

VB.Net

Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.Reporting.WinForms
Imports System.Drawing.Printing

Code

C#

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;
                }
            }
        }
    }
}

private void Print(object sender, EventArgs e)
{
    this.reportViewer1.PrintDialog();
    PageSetupDialog setupDlg = new PageSetupDialog();
    PrintDocument printDoc = new PrintDocument();
    setupDlg.Document = printDoc;
    setupDlg.AllowMargins = false;
    setupDlg.AllowOrientation = false;
    setupDlg.AllowPaper = false;
    setupDlg.AllowPrinter = false;
    setupDlg.Reset();
    printDoc.DefaultPageSettings.PaperSize = new PaperSize("A5", 850, 1400);
}

VB.Net

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

Private Sub Print(sender As System.Object, e As System.EventArgs) Handles btnPrint.Click
    Me.ReportViewer1.PrintDialog()
    Dim setupDlg As PageSetupDialog = New PageSetupDialog()
    Dim printDoc As PrintDocument = New PrintDocument()
    setupDlg.Document = printDoc
    setupDlg.AllowMargins = False
    setupDlg.AllowOrientation = False
    setupDlg.AllowPaper = False
    setupDlg.AllowPrinter = False
    setupDlg.Reset()
    printDoc.DefaultPageSettings.PaperSize = New PaperSize("A5", 850, 1400)
End Sub

Screenshot