Grid View for ASP.NET Web Forms - How to add content in the header and footer sections when exporting grid
The DevExpress.XtraPrinting library allows you to add a header and footer in an exported document in two ways:
- Create a printing link for each element (header, footer, and main control) and combine them into one document.
- Create a printing link for the main control and handle the link's CreateReportHeaderArea and CreateReportFooterArea events to customize a document's header and footer.
In this example, the first approach is used to create a document header, and the second to create a document footer.
Printing links transforms a control's data into bricks of appropriate types, and arranges them into a printing document. To create a printing link, follow the steps below:
- Create a LinkBase object for the header area.
LinkBase header = new LinkBase();
- Handle its CreateDetailHeaderArea event.
header.CreateDetailHeaderArea += Header_CreateDetailHeaderArea;
- Use the event properties to render additional objects (images, text) in the header area:
void Header_CreateDetailHeaderArea(object sender, CreateAreaEventArgs e) { e.Graph.BorderWidth = 0; Rectangle r = new Rectangle(0, 0, headerImage.Width, headerImage.Height); e.Graph.DrawImage(headerImage, r); r = new Rectangle(0, headerImage.Height, 200, 50); e.Graph.DrawString("Additional Header information here....", r); }
-
Create a PrintableComponentLinkBase object and assign the ASPxGridView control to the Component property.
PrintableComponentLinkBase link1 = new PrintableComponentLinkBase(); link1.Component = ASPxGridView1;
-
Handle the CreateReportFooterArea event to customize grid footer.
link1.CreateReportFooterArea += Link1_CreateReportFooterArea; // ... void Link1_CreateReportFooterArea(object sender, CreateAreaEventArgs e) { e.Graph.BorderWidth = 0; Rectangle r = new Rectangle(0, 20, 200, 50); e.Graph.Font = new Font("Times New Roman", 12, FontStyle.Italic); e.Graph.ForeColor = Color.Gray; e.Graph.DrawString("This is footer", r); }
-
Create a CompositeLinkBase object and combine printable elements (header and the grid control).
CompositeLinkBase compositeLink = new CompositeLinkBase(ps); compositeLink.Links.AddRange(new object[] { header, link1 }); compositeLink.CreateDocument();
-
Call a ExportTo[FORMAT] method to export the result.
using (MemoryStream stream = new MemoryStream()) { switch (format) { case "xls": compositeLink.ExportToXls(stream); WriteToResponse("filename", true, format, stream); break; case "pdf": compositeLink.ExportToPdf(stream); WriteToResponse("filename", true, format, stream); break; case "rtf": compositeLink.ExportToRtf(stream); WriteToResponse("filename", true, format, stream); break; default: break; } }
- Default.aspx (VB: Default.aspx)
- Default.aspx.cs (VB: Default.aspx.vb)