In this article I will explicate with an example, how to impress DataGridView in Windows Awarding using C# and VB.Net.

The DataGridView will exist printed with the help of PrintPreviewDialog control in Windows Application using C# and VB.Net.

Form Controls

In the below Grade, there's a DataGridView, a Print Button, a PrintPreviewDialog and a PrintDocument controls.

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

Populating DataGridView

In order to populate the DataGridView, I accept created a dynamic DataTable with some sample data.

C#

private void Form1_Load(object sender, EventArgs eastward)

{

this.BindDataGridView();

}

private void BindDataGridView()

{

DataTable dt = new DataTable();

    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),

new DataColumn("Name", typeof(cord)),

new DataColumn("Country",typeof(string)) });

    dt.Rows.Add(1, "John Hammond", "U.s.a.");

    dt.Rows.Add together(2, "Mudassar Khan", "India");

    dt.Rows.Add together(3, "Suzanne Mathews", "France");

    dt.Rows.Add(4, "Robert Schidner", "Russian federation");

this.dataGridView1.DataSource = dt;

}

VB.Net

Private Sub Form1_Load(sender Equally System.Object, east As System.EventArgs) Handles MyBase.Load

    BindDataGridView()

Finish Sub

Private Sub BindDataGridView()

Dim dt Equally New DataTable()

    dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Proper name", GetType(String)), New DataColumn("Country", GetType(Cord))})

    dt.Rows.Add(ane, "John Hammond", "The states")

    dt.Rows.Add(2, "Mudassar Khan", "Bharat")

    dt.Rows.Add(3, "Suzanne Mathews", "France")

    dt.Rows.Add(4, "Robert Schidner", "Russia")

Me.dataGridView1.DataSource = dt

Terminate Sub

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

Press DataGridView

When the Print Push is clicked, first the DataGridView height is adapted based on the count of rows so that all rows of the DataGridView are printed.

A Bitmap object is created and the DataGridView is fatigued in to the Bitmap object.

Once the DataGridView is drawn, the DataGridView is resized dorsum to its original height.

Finally the PrintPreviewDialog is shown and the Bitmap object is printed.

C#

Bitmap bitmap;

private void btnPrint_Click(object sender, EventArgs east)

{

//Resize DataGridView to total meridian.

int height = dataGridView1.Summit;

    dataGridView1.Height = dataGridView1.RowCount * dataGridView1.RowTemplate.Height;

//Create a Bitmap and draw the DataGridView on it.

    bitmap = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);

    dataGridView1.DrawToBitmap(bitmap, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Acme));

//Resize DataGridView dorsum to original peak.

    dataGridView1.Meridian = tiptop;

//Prove the Print Preview Dialog.

    printPreviewDialog1.Document = printDocument1;

    printPreviewDialog1.PrintPreviewControl.Zoom = 1;

    printPreviewDialog1.ShowDialog();

}

private void PrintPage(object sender, Organization.Drawing.Printing.PrintPageEventArgs e)

{

//Print the contents.

    e.Graphics.DrawImage(bitmap, 0, 0);

}

VB.Net

Individual bitmap As Bitmap

Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click

'Resize DataGridView to full height.

Dim pinnacle As Integer = dataGridView1.Superlative

    dataGridView1.Height = dataGridView1.RowCount * dataGridView1.RowTemplate.Height

'Create a Bitmap and draw the DataGridView on it.

    bitmap = New Bitmap(Me.dataGridView1.Width, Me.dataGridView1.Height)

    dataGridView1.DrawToBitmap(bitmap, New Rectangle(0, 0, Me.dataGridView1.Width, Me.dataGridView1.Height))

'Resize DataGridView back to original acme.

    dataGridView1.Superlative = height

'Show the Print Preview Dialog.

    printPreviewDialog1.Document = printDocument1

    printPreviewDialog1.PrintPreviewControl.Zoom = 1

    printPreviewDialog1.ShowDialog()

End Sub

Individual Sub PrintPage(sender As Object, e As System.Drawing.Press.PrintPageEventArgs) Handles printDocument1.PrintPage

'Impress the contents.

    e.Graphics.DrawImage(bitmap, 0, 0)

End Sub

Screenshots

Print Preview Dialog displaying the DataGridView

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

Printed DataGridView

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

Downloads