Windows Forms: Print DataGridView with Header & Footer with Landscape in C#
By FoxLearn 6/19/2017 9:33:36 PM 17.19K
Print data from DataGridView with Header & Footer with Landscape
Step 1: 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
Step 2: 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
Step 3: Add code to handle your form as below
using DGVPrinterHelper; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace PrintDataGridViewHeaderFooter { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { using(NorthwindEntities db = new NorthwindEntities()) { //Get data from northwind database customerBindingSource.DataSource = db.Customers.ToList(); } } 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 TUTORIALS
- How to Add Combobox to DataGridView in C#
- How to Create Multiple pages on the Form using Panel control in C#
- How to insert Math Equation to RichTextBox in C#
- How to Send and Receive email in Microsoft Outlook using C#
- How to Print Windows Form in C#
- How to Use Form Load and Button click Event in C#
- How to use Advanced Filter DataGridView in C#
- How to use TagListControl in C#
Categories
Popular Posts
How to implement Jint in C#
09/14/2024
How to Download Chromedriver for Selenium
09/14/2024