ElementExportingToDocument and ElementExportedToDocument
You have more control over the exportation by utilizing the export events which are fired when you export data from RadGridView.
Export Events Life Cycle
The purpose of the events is to provide you with a mechanism to style or modify the exported data.
When you call the ExportToXlsx, ExportToPdf, ExportToWorkbook and ExportToRadFixedDocument methods of RadGridView, the ElementExportingToDocument and ElementExportedToDocument events are fired.
The ElementExportingToDocument event can be cancelled for a particular row or cell. If it is not cancelled, the ElementExportedToDocument event is fired for the associated RadGridView.
You can subscribe to the events either declaratively or from the code-behind like this:
Example 1: Subscribe to events declaratively:
<telerik:RadGridView x:Name="RadGridView1"
ElementExportingToDocument="RadGridView1_ElementExportingToDocument"
ElementExportedToDocument="RadGridView1_ElementExportedToDocument" />
Example 2: Subscribe to events from the code-behind:
RadGridView1.ElementExportingToDocument += RadGridView1_ElementExportingToDocument;
RadGridView1.ElementExportedToDocument += RadGridView1_ElementExportedToDocument;
AddHandler RadGridView1.ElementExportingToDocument, AddressOf RadGridView1_ElementExportingToDocument
AddHandler RadGridView1.ElementExportedToDocument, AddressOf RadGridView1_ElementExportedToDocument
ElementExportingToDocument
The event handler expects GridViewElementExportingToDocumentEventArgs argument that has the following properties:
- Element - Gets the current element that is about to be exported.
- Cancel - Gets or sets a boolean value that indicates whether the event should be canceled or not.
- DataContext - the DataContext of the corresponding visual element. For example the DataContext of the ExportElement.Row is the underlying GridViewRow.
- Value - the value that is about to be exported.
- VisualParameters - they are of type GridViewDocumentVisualExportParameters and have a property Style which is of type CellSelectionStyle. It provides the ability to set the FontSize, Fill and etc. for the exported document.
ElementExportedToDocument
The event handler expects GridViewElementExportedToDocumentEventArgs argument that has the following properties:
- Element - the export Element.
- DataContext - the DataContext of the corresponding element.
InitializingExcelMLStyles (ExcelML only)
This event will be only raised when exporting with ExportFormat.ExcelML
You can define a Style when InitializingExcelMLStyles event is raised. For example:
Example 6: Define a style:
ExcelMLStyle style = new ExcelMLStyle("0");
style.Alignment.Horizontal = ExcelMLHorizontalAlignment.Automatic;
e.Styles.Add(style);
Alignment:
- Horizontal - you can choose a specific alignment through ExcelMLHorizontalAlignment enumeration.
- Vertical - you can choose a specific alignment through ExcelMLVerticalAlignment enumeration.
- Indent
- Rotate
- ShrinkToFit
- VerticalText
- WrapText
Font:
- Bold
- Color
- FontName
- Italic
- Outline (only for Mac)
- Size
- StrikeThrough
- Underline - you can choose a specific underline through ExcelMLUnderline
Interior:
- Color - You must also set Pattern in order to be applied Interior.Color
- Pattern - you can choose a specific pattern through ExcelMLPattern
- PatternColor - You must also set Pattern in order to be applied Interior.PatternColor
NumberFormat:
- Format
Example 7: Apply a style before exporting:
private void clubsGrid_InitializingExcelMLStyles_1(object sender, ExcelMLStylesEventArgs e)
{
ExcelMLStyle style = new ExcelMLStyle("0");
// Alignment
style.Alignment.Horizontal = ExcelMLHorizontalAlignment.Automatic;
style.Alignment.Vertical = ExcelMLVerticalAlignment.Top;
style.Alignment.Indent = 5;
style.Alignment.Rotate = 0;
style.Alignment.ShrinkToFit = true;
style.Alignment.VerticalText = true;
style.Alignment.WrapText = true;
// Font
style.Font.Bold = true;
style.Font.Color = "Beige";
style.Font.FontName = "Calibri";
style.Font.Italic = true;
style.Font.Outline = true;
style.Font.Shadow = true;
style.Font.Size = 10;
style.Font.StrikeThrough = true;
style.Font.Underline = ExcelMLUnderline.Double;
// Interior
style.Interior.Color = "Green";
style.Interior.Pattern = ExcelMLPattern.Solid;
style.Interior.PatternColor = "#FF0000";
// NumberFormat
style.NumberFormat.Format = "00.00";
e.Styles.Add(style);
}