RadGridView Excel Export - Issue with Footer Export

1 Answer 85 Views
GridView
Rss
Top achievements
Rank 1
Rss asked on 23 May 2023, 11:16 AM | edited on 23 May 2023, 11:17 AM

I am utilizing the RadGridView's export to Excel functionality, and I have encountered an issue where the footer of my grid is not being exported correctly.

In my implementation, I have a Footer for the GridViewDataColumn that uses a TextBlock control, and I have applied a binding to the Text property of this TextBlock. This TextBlock shows up perfectly when viewing the grid within the application. However, when I export the grid to Excel, the content of the TextBlock from the Footer appears in the Excel file as "System.Windows.Controls.TextBlock" rather than the actual expected value.

Here is the XAML for the GridViewDataColumn footer:

<telerik:GridViewDataColumn.Footer>
    <TextBlock
        Text="{Binding Path=ItemsSource, RelativeSource={RelativeSource AncestorType=telerik:RadGridView}, Converter={StaticResource QiYesValueToCountConverter}, ConverterParameter=IK_2_1_2, UpdateSourceTrigger=PropertyChanged}"
        FontSize="20"
        ToolTip="{Binding ElementName=UserControl1, Path=DataContext.IkItemsSummary.IK_2_1_2}" />
</telerik:GridViewDataColumn.Footer>

And here is the method I use for the export:

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
    string extension = "xlsx";
    SaveFileDialog dialog = new SaveFileDialog()
    {
        DefaultExt = extension,
        Filter = String.Format("{1} Dateien (*.{0})|*.{0}", extension, "Excel"),
        FilterIndex = 1,
        FileName = "QI-Auswertungen"
    };
    if (dialog.ShowDialog() == true)
    {
        using (Stream stream = dialog.OpenFile())
        {
            RadGridView1.ExportToXlsx(stream);
        }
    }
}

I have tried a few potential solutions, such as using a ContentControl instead of a TextBlock and tried using the ElementExporting and ElementExported events to manipulate the exported value directly. However, I am still facing the same issue.

1 Answer, 1 is accepted

Sort by
0
Stenly
Telerik team
answered on 26 May 2023, 10:36 AM

Hello,

To achieve this requirement, you could add a new event handler for the ElementExportingToDocument event of RadGridView. Then, if the e.Element is of the type ExportElement.FooterCell, modify the e.Value property.

The following code snippets show this suggestion's implementation:

<telerik:RadGridView x:Name="radGridView" 
                     ItemsSource="{Binding People}"
                     ShowColumnFooters="True" 
                     AutoGenerateColumns="False"
                     ElementExportingToDocument="radGridView_ElementExportingToDocument">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"/>
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Age}" >
            <telerik:GridViewDataColumn.Footer>
                <TextBlock Text="My Footer Value"/>
            </telerik:GridViewDataColumn.Footer>
        </telerik:GridViewDataColumn>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>
private void radGridView_ElementExportingToDocument(object sender, GridViewElementExportingToDocumentEventArgs e)
{
    if (e.Element == ExportElement.FooterCell)
    {
        if (e.Value != null)
        {
            if (e.Value is TextBlock textBlock)
            {
                e.Value = textBlock.Text;
            }
        }   
    }
}

The produced result is as follows:

I have attached a sample project for you to test.

Regards,
Stenly
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
GridView
Asked by
Rss
Top achievements
Rank 1
Answers by
Stenly
Telerik team
Share this question
or