Skip to content

DevExpress-Examples/asp-net-web-forms-grid-export-display-content-in-header-and-footer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

In this example, the first approach is used to create a document header, and the second to create a document footer.

1. Create a header as a separate printing link

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:

  1. Create a LinkBase object for the header area.
    LinkBase header = new LinkBase();
  2. Handle its CreateDetailHeaderArea event.
    header.CreateDetailHeaderArea += Header_CreateDetailHeaderArea;
  3. 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 footer in the grid's event handler

  1. Create a PrintableComponentLinkBase object and assign the ASPxGridView control to the Component property.

    PrintableComponentLinkBase link1 = new PrintableComponentLinkBase();
    link1.Component = ASPxGridView1;
  2. 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);
    }

Combine printing links and export the result

  1. 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();
  2. 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;
        }
    }

Files to Review

More Examples