How To Print Datagridview In C Sharp

How To Print Datagridview In C# – PrintDialog

How To Print Datagridview In C# – PrintDialog

How To Print Datagridview Data In C# Windows Application
This Tutorial Shows How To Print Datagridview Data In C# Windows Application Using Visual Studio 2010 Integrated development environment

How To Print Datagridview In C Sharp
How To Print Datagridview In C Sharp
How To Print Datagridview In CSharp
How To Print Datagridview In CSharp

Implementing Print Feature in datagridview


Open/Start Visual Studio 2010.
Create a new C# Windows Form project and give an appropriate name.
Drag and drop datagridview in your form from the toolbox.
Populate/Add data in the datagridview. In This Case Am Adding static rows.
Drag and drop the printDocument from the toolbox.
Add a button for printing datagridview on the form.
Double click to get the click event of a button.
Write printing code in the print button click event.

printDataGridViewDocument1.Print();


Double click on the printDocument to get the Print Page event.
Write the appropriate code for printing the datagridview

Form1.cs SOURCE CODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HowToPrintDataGridView
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Increase DataGridView Row Height
            printDataGridViewData1.RowTemplate.Height = 50;
            //Populate DataGridView Rows
            Image printDataGridViewDataImage1 = Image.FromFile("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg");
            printDataGridViewData1.Rows.Add("Joel Anderson", "Joe@emailextension.com", "+1999999999", "English", "United States Of America", "Male", "C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg", printDataGridViewDataImage1);
        }

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

        private void button1_Click(object sender, EventArgs e)
        {
            PrintDialog printDataGridViewDocumentPrintDialog = new PrintDialog();
            printDataGridViewDocumentPrintDialog.Document = printDataGridViewDocument1;
            printDataGridViewDocumentPrintDialog.UseEXDialog = true;

            DialogResult printDataGridViewDocumentDialogResult = printDataGridViewDocumentPrintDialog.ShowDialog();

            //If Print is clicked
            if (printDataGridViewDocumentDialogResult == DialogResult.OK)
            {
                printDataGridViewDocument1.DocumentName = "How To Print DataGridView";
                printDataGridViewDocument1.Print();
            }
        }
    }
}

Form1.Designer.cs SOURCE CODE

namespace HowToPrintDataGridView
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.printDataGridViewData1 = new System.Windows.Forms.DataGridView();
            this.button1 = new System.Windows.Forms.Button();
            this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column5 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column6 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column7 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column8 = new System.Windows.Forms.DataGridViewImageColumn();
            this.printDataGridViewDocument1 = new System.Drawing.Printing.PrintDocument();
            ((System.ComponentModel.ISupportInitialize)(this.printDataGridViewData1)).BeginInit();
            this.SuspendLayout();
            // 
            // printDataGridViewData1
            // 
            this.printDataGridViewData1.AllowUserToAddRows = false;
            this.printDataGridViewData1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            this.printDataGridViewData1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.printDataGridViewData1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.Column1,
            this.Column2,
            this.Column3,
            this.Column4,
            this.Column5,
            this.Column6,
            this.Column7,
            this.Column8});
            this.printDataGridViewData1.Dock = System.Windows.Forms.DockStyle.Top;
            this.printDataGridViewData1.Location = new System.Drawing.Point(0, 0);
            this.printDataGridViewData1.Name = "printDataGridViewData1";
            this.printDataGridViewData1.Size = new System.Drawing.Size(755, 310);
            this.printDataGridViewData1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button1.Location = new System.Drawing.Point(481, 316);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(274, 32);
            this.button1.TabIndex = 1;
            this.button1.Text = "Print DataGridView";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Column1
            // 
            this.Column1.HeaderText = "Full Name";
            this.Column1.Name = "Column1";
            // 
            // Column2
            // 
            this.Column2.HeaderText = "Email";
            this.Column2.Name = "Column2";
            // 
            // Column3
            // 
            this.Column3.HeaderText = "Phone Number";
            this.Column3.Name = "Column3";
            // 
            // Column4
            // 
            this.Column4.HeaderText = "Language";
            this.Column4.Name = "Column4";
            // 
            // Column5
            // 
            this.Column5.HeaderText = "Country";
            this.Column5.Name = "Column5";
            // 
            // Column6
            // 
            this.Column6.HeaderText = "Gender";
            this.Column6.Name = "Column6";
            // 
            // Column7
            // 
            this.Column7.HeaderText = "Image Path";
            this.Column7.Name = "Column7";
            // 
            // Column8
            // 
            this.Column8.HeaderText = "Image";
            this.Column8.ImageLayout = System.Windows.Forms.DataGridViewImageCellLayout.Stretch;
            this.Column8.Name = "Column8";
            // 
            // printDataGridViewDocument1
            // 
            this.printDataGridViewDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDataGridViewDocument1_PrintPage);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(755, 352);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.printDataGridViewData1);
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.printDataGridViewData1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.DataGridView printDataGridViewData1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column3;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column4;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column5;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column6;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column7;
        private System.Windows.Forms.DataGridViewImageColumn Column8;
        private System.Drawing.Printing.PrintDocument printDataGridViewDocument1;
    }
}


Program.cs SOURCE CODE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace HowToPrintDataGridView
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Leave a Reply

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