How to Print DataGridView with Header & Footer with Landscape in C#
By FoxLearn 11/16/2024 1:30:44 PM 17.58K
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 update UI from another thread in C#
- How to get CheckedListBox selected values in C#
- How to use Advanced Filter DataGridView in C#
- How to create a Notification Popup in C#
- How to Use Form Load and Button click Event in C#
- How to Link Chart /Graph with Database in C#
- How to Check SQL Server Connection in C#
- How to Generate Serial Key in C#