C Sharp And Ms Access Database Tutorial 18 How To Print Datagridview

C# And Ms Access Database Tutorial #18 – How To Print Datagridview In C# Windows Application

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #18 – How To Print Datagridview In C# Windows Application

This Tutorial Shows How To Print Datagridview In C# Windows Forms Application using visual studio 2010.

C# SOURCE CODE

        private void printDocumentDataGridView1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Bitmap printDocumentDataGridViewBitmap = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
            dataGridView1.DrawToBitmap(printDocumentDataGridViewBitmap, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));
            e.Graphics.DrawImage(printDocumentDataGridViewBitmap, 0, 0);
        }

        //Print DataGridView
        private void btnPrintDataGridView_Click(object sender, EventArgs e)
        {
            try
            {
                PrintDialog printDataGridViewPrintDialog = new PrintDialog();
                //Document
                printDataGridViewPrintDialog.Document = printDocumentDataGridView1;

                printDataGridViewPrintDialog.UseEXDialog = true;
                //Dialog Result
                DialogResult printDataGridViewPrintDialogResult = printDataGridViewPrintDialog.ShowDialog();

                if (printDataGridViewPrintDialogResult == DialogResult.OK)
                {
                    //Document Name
                    printDocumentDataGridView1.DocumentName = "Print DataGridView";
                    //Print Function
                    printDocumentDataGridView1.Print();
                    //Output Message After Printing DGV Successfully 
                    MessageBox.Show("DataGridView Printed Successfully......................... ", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Leave a Reply

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