Print each DataGridView row separately using C# and VB.Net in Windows Application

Last Reply 19 days ago By arunkurmi

Posted 19 days ago

How to print separate print (each page for each row)  for each row in datagridview

You are viewing reply posted by: arunkurmi 19 days ago.
Posted 19 days ago

Hi satabeach,

Check this sample. now take its reference.

Namespaces

C#

using System.Data;
using System.Drawing;

VB.Net

Imports System.Data
Imports System.Drawing

 Code

C#

Bitmap bitmap;
private void Form1_Load(object sender, EventArgs e)
{
    BindDataGrid();
}

private void BindDataGrid()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] { new DataColumn("ID"), new DataColumn("Name"), new DataColumn("Country") });
    dt.Rows.Add(1, "John Hammond", "United States");
    dt.Rows.Add(2, "Mudassar Khan", "India");
    dt.Rows.Add(3, "Suzanne Mathews", "France");
    dt.Rows.Add(4, "Robert Schidner", "Russia");
    this.dataGridView1.DataSource = dt;
    this.dataGridView1.AllowUserToAddRows = false;
}

private void btnPrint_Click(object sender, EventArgs e)
{
    int i = 0;
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        for (int j = i; j < dataGridView1.Rows.Count; j++)
        {
            if (j != i)
            {
                dataGridView1.Rows[j].Visible = false;
            }
            else
            {
                dataGridView1.Rows[j].Visible = true;
            }
        }

        int height = 45;
        dataGridView1.Height = dataGridView1.RowCount * dataGridView1.RowTemplate.Height;
        bitmap = new Bitmap(this.dataGridView1.Width, 45);
        dataGridView1.DrawToBitmap(bitmap, new Rectangle(0, 0, this.dataGridView1.Width, 45));
        dataGridView1.Height = height;
        printPreviewDialog1.Document = printDocument1;
        printPreviewDialog1.PrintPreviewControl.Zoom = 1;
        printPreviewDialog1.ShowDialog();
        for (int k = i; k < dataGridView1.Rows.Count; k++)
        {
            if (k == i)
            {
                CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                currencyManager1.SuspendBinding();
                dataGridView1.Rows[k].Visible = false;
            }
            else
            {
                dataGridView1.Rows[k].Visible = true;
            }
        }
        i++;
    }
}

private void print_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    e.Graphics.DrawImage(bitmap, 0, 0);
}

VB.Net

Private bitmap As Bitmap
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    BindDataGrid()
End Sub

Private Sub BindDataGrid()
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn() {New DataColumn("ID"), New DataColumn("Name"), New DataColumn("Country")})
    dt.Rows.Add(1, "John Hammond", "United States")
    dt.Rows.Add(2, "Mudassar Khan", "India")
    dt.Rows.Add(3, "Suzanne Mathews", "France")
    dt.Rows.Add(4, "Robert Schidner", "Russia")
    Me.dataGridView1.DataSource = dt
    Me.dataGridView1.AllowUserToAddRows = False
End Sub

Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPrint.Click
    Dim i As Integer = 0
    For Each row As DataGridViewRow In dataGridView1.Rows
        For j As Integer = i To dataGridView1.Rows.Count - 1
            If j <> i Then
                dataGridView1.Rows(j).Visible = False
            Else
                dataGridView1.Rows(j).Visible = True
            End If
        Next
        Dim height As Integer = 45
        dataGridView1.Height = dataGridView1.RowCount * dataGridView1.RowTemplate.Height
        bitmap = New Bitmap(Me.dataGridView1.Width, 45)
        dataGridView1.DrawToBitmap(bitmap, New Rectangle(0, 0, Me.dataGridView1.Width, 45))
        dataGridView1.Height = height
        printPreviewDialog1.Document = printDocument1
        printPreviewDialog1.PrintPreviewControl.Zoom = 1
        printPreviewDialog1.ShowDialog()
        For k As Integer = i To dataGridView1.Rows.Count - 1
            If k = i Then
                Dim currencyManager1 As CurrencyManager = CType(BindingContext(dataGridView1.DataSource), CurrencyManager)
                currencyManager1.SuspendBinding()
                dataGridView1.Rows(k).Visible = False
            Else
                dataGridView1.Rows(k).Visible = True
            End If
        Next
        i += 1
    Next
End Sub

Private Sub print_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printDocument1.PrintPage
    e.Graphics.DrawImage(bitmap, 0, 0)
End Sub