This is a migrated thread and some comments may be shown as answers.

Issue with exporting ColumnGroups to Excel

2 Answers 144 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
RD
Top achievements
Rank 1
RD asked on 18 Jul 2012, 03:44 AM
Hi,

I am trying to export the content of a RadGridView to Excel and everything works fine except for Column Groups.
With the column groups what ends up being exported is their Name property and not the actual value displayed on screen.

Here is a sample XAML I put together to simulate my scenario:
<TextBlock x:Name="HeaderText" Visibility="Collapsed" Text="Person Details" />
 
<telerik:RadGridView x:Name="PersonsGrid" AutoGenerateColumns="False" ItemsSource="{Binding List}">
    <telerik:RadGridView.ColumnGroups>
        <telerik:GridViewColumnGroup Name="ColumnGroup1">
            <telerik:GridViewColumnGroup.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Text, ElementName=HeaderText}" TextAlignment="Center" FontWeight="Bold" />
                </DataTemplate>
            </telerik:GridViewColumnGroup.HeaderTemplate>
        </telerik:GridViewColumnGroup>
    </telerik:RadGridView.ColumnGroups>
     
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn Header="ID" DataMemberBinding="{Binding ID}" TextAlignment="Center" />
        <telerik:GridViewDataColumn Header="Name" DataMemberBinding="{Binding Name}" ColumnGroupName="ColumnGroup1" />
        <telerik:GridViewDataColumn Header="Age" DataMemberBinding="{Binding Age}" TextAlignment="Center" ColumnGroupName="ColumnGroup1" />
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

In this particular example I have got one column group and what I find exported in Excel is the Name of this column group, i.e. "ColumnGroup1", but I would like to have instead the Text of the TextBlock in the HeaderTemplate, which is the actual value displayed on screen for this column group, i.e. "Person Details".

Is there any way I can achieve this? Perhaps using the ElementExporting/ElementExported events?

I am using Silverlight 4 and your suite of Silverlight controls v2012.1.326.1040.

Thanks,
Raoul 

2 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 23 Jul 2012, 12:52 PM
Hello,

Indeed you are right. This is the expected behaviour when exporting the common headers.

Here is a sample event handler you may adapt to your case :
void RadGridView1_ElementExporting(object sender, GridViewElementExportingEventArgs e)
{
    if (e.Element == ExportElement.CommonColumnHeader)
    {
        CommonColumnHeader header = ((RadGridView)sender).ChildrenOfType<CommonColumnHeader>().Where(c => (String)c.Content == e.Value.ToString()).First();
 
        if(header != null && !String.IsNullOrEmpty(header.Content.ToString()))
            e.Value = header.ChildrenOfType<TextBlock>().First().Text;
    }
}

Basically it finds the relevant header in the visual tree and reads the textblock within.

*To have access to the ChildrenOfType extension method please add : using Telerik.Windows.Controls in your cs.file.

All the best,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
RD
Top achievements
Rank 1
answered on 24 Jul 2012, 01:09 AM
Hi Pavel,

Excellent, that is exactly what I needed.

Thanks,
Raoul
Tags
GridView
Asked by
RD
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
RD
Top achievements
Rank 1
Share this question
or