Edit this page

Export Custom Header

PROBLEM

When exporting RadGridView and you have defined custom headers for some columns, the actual text within the custom header is not exported correctly unless you explicitly define the value.

CAUSE

When you insert a custom UIElement in the header of a GridViewColumn, the RadGridView does not know the meaningful value of the UIElement.

SOLUTION

In case you insert a custom UIElement in the header of a GridViewColumn, you will need to handle the ElementExporting event of RadGridView. Then check if the exported element is a header cell and set the appropriate value to be exported. This is needed since there is no way for the RadGridView to know the meaningful value of a UIElement.

Here is a small sample code:

  • Define a custom header with a TextBlock

Example 1: Define a custom header:

<telerik:GridViewDataColumn DataMemberBinding="{Binding StadiumCapacity}">
    <telerik:GridViewDataColumn.Header>
        <TextBlock Text="Stadium capacity (seats)"
        TextWrapping="Wrap"/>
    </telerik:GridViewDataColumn.Header>
</telerik:GridViewDataColumn>
  • Handle the ElementExporting event for the exported RadGridView

Example 2: Handle the ElementExporting event:

private void clubsGrid_ElementExporting(object sender, GridViewElementExportingEventArgs e)
{
    if (e.Value != null && e.Value.GetType() == typeof(TextBlock))
        e.Value = (e.Value as TextBlock).Text;
}
Private Sub clubsGrid_ElementExporting(sender As Object, e As GridViewElementExportingEventArgs)
    If e.Value IsNot Nothing AndAlso e.Value.GetType = GetType(TextBlock) Then
        e.Value = TryCast(e.Value, TextBlock).Text
    End If
End Sub

When you set the Value in the GridViewElementExportingEventArgs object to the correct header text, the headers will be exported as expected.