How to Print DataGridView with Header & Footer with Landscape in C#
By FoxLearn Published on Jun 19, 2017 18.34K
The DGVPrinter is a popular third-party library that makes printing DataGridView
content much easier and provides support for headers, footers, and various page layout options like landscape orientation.
How to Print DataGridView with Header Footer with Landscape in C#?
Open Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "PrintDataGridViewHeaderFooter" and then click OK
You need to download and install the DGVPrinter library. The DGVPrinter
class allows you to configure various settings such as headers, footers, and page layout (landscape).
Drag and drop the DataGridView and Button controls from Visual toolbox onto your form designer, then design your form as below.
We use the Northwind database to play demo. If you haven't got Northwind database, you can view How to download and restore Northwind database in SQL Server
Add an EF model to your project as below
Add code to handle your form as shown below.
private void Form1_Load(object sender, EventArgs e) { using (NorthwindEntities db = new NorthwindEntities()) { //Get data from northwind database customerBindingSource.DataSource = db.Customers.ToList(); } } // print datagridwiew with header private void btnPrint_Click(object sender, EventArgs e) { // Init print datagridview in c# DGVPrinter printer = new DGVPrinter(); printer.Title = "Customer Report";//Header printer.SubTitle = string.Format("Date: {0}", DateTime.Now.Date.ToString("MM/dd/yyyy")); printer.SubTitleFormatFlags = StringFormatFlags.LineLimit | StringFormatFlags.NoClip; printer.PageNumbers = true; printer.PageNumberInHeader = false; printer.PorportionalColumns = true; printer.HeaderCellAlignment = StringAlignment.Near; printer.Footer = "FoxLearn";//Footer printer.FooterSpacing = 15; //Print landscape mode printer.printDocument.DefaultPageSettings.Landscape = true; printer.PrintDataGridView(dataGridView); }
To play demo you need to download DGVPrinter class, then copy to your project.
VIDEO TUTORIAL
- How to Open and Show a PDF file in C#
- How to Get all Forms and Open Form with Form Name in C#
- How to zoom an image in C#
- How to Print a Picture Box in C#
- How to update UI from another thread in C#
- How to Search DataGridView by using TextBox in C#
- How to read and write to text file in C#
- How to save files using SaveFileDialog in C#