How to Print DataGridView with Header & Footer with Landscape in C#
By FoxLearn 11/16/2024 1:30:44 PM 17.31K
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 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 Display Images in DataGridView in C#
- How to Create a custom Progress Bar with Percentage in C#
- How to read an image file in C#
- How to use BackgroundWorker in C#
- How to protect .NET code from reverse engineering
- How to Get value from another Form in C#
- How to make a Notepad in C#
- How to Receive SMS from WhatsApp in C#