How To Print DataGridView In C# Windows Application

How To Print DataGridView In C# Windows Forms Application

How To Print DataGridView In C# Windows Application
How To Print Datagridview In C Sharp Windows Forms Application
How To Print Datagridview In C Sharp Windows Forms Application

This Tutorial Shows How To Print DataGridView In C# Windows Forms Application.

Print Options

How To Print DataGridView Print Options
How To Print DataGridView Print Options

Print Preview

How To Print DataGridView Print Preview
How To Print DataGridView Print Preview

Printed DataGridView Document

How To Print DataGridView Printed Document
How To Print DataGridView Printed Document

The data to be printed is loaded to the DataGridView from excel. Document is printed when you click print button, But print preview dialog box appears first, just immediately after you click print button.

C# SOURCE CODE 1

        private void printDocument1_PrintPage_1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //create Bitmap and add/draw datagridview to it 
            Bitmap dataGridViewBitmap = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);

            dataGridView1.DrawToBitmap(dataGridViewBitmap, new System.Drawing.Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));

            e.Graphics.DrawImage(dataGridViewBitmap, 0, 0);
        }

C# SOURCE CODE 2

        private void button7_Click(object sender, EventArgs e)
        {
            PrintDialog daraGridViewPrintDialog = new PrintDialog();
            daraGridViewPrintDialog.Document = printDocument1;
            daraGridViewPrintDialog.UseEXDialog = true;

            if (DialogResult.OK == daraGridViewPrintDialog.ShowDialog())
            {
                //Document Name
                printDocument1.DocumentName = "Printed DataGridView";
                //Print Datagridview
                printDocument1.Print();

            }

            ////Preview Before Printing
            //PrintPreviewDialog dataGridViewPrintPreviewDialog = new PrintPreviewDialog();
            //dataGridViewPrintPreviewDialog.Document = printDocument1;
            //dataGridViewPrintPreviewDialog.ShowDialog();

        }

Leave a Reply

Your email address will not be published. Required fields are marked *