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

Custom column not exported to excel

3 Answers 113 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Deborah
Top achievements
Rank 1
Deborah asked on 18 May 2012, 11:17 AM
Hi,
in my gridview I've added a custom column showing the row number, as shown in this example:
http://demos.telerik.com/silverlight/#GridView/RowNumber

Here's the code:
<telerik:RadGridView.ColumnGroups>
                    <telerik:GridViewColumnGroup Name="Identificazione" Header="IDENTIFICAZIONE></telerik:GridViewColumnGroup>
                    <telerik:GridViewColumnGroup Name="...
</telerik:RadGridView.ColumnGroups>
                <telerik:RadGridView.Columns>
                    <custom:MyColumn Header="#" Width="50"  Background="#7F2FB3FD"/>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Codice}"
                                                Header="Codice"
                                                IsReadOnly="True"
                                                Width="81"
                                                ColumnGroupName="Identificazione"/>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Descrizione}"
                                                Header="Descrizione"
                                                IsReadOnly="True"
                                                Width="145"
                                                ColumnGroupName="Identificazione"/>
                    <telerik:GridViewDataColumn...

public class MyColumn : Telerik.Windows.Controls.GridViewColumn
    {
        public override FrameworkElement CreateCellElement(Telerik.Windows.Controls.GridView.GridViewCell cell, object dataItem)
        {
            TextBlock textBlock = cell.Content as TextBlock;
 
            if (textBlock == null)
            {
                textBlock = new TextBlock();
            }
 
            textBlock.Text = (this.DataControl.Items.IndexOf(dataItem) + 1).ToString();
 
            return textBlock;
        }
    }

When I export the gridview to excel the custom column is not exported.
This is not a problem for me, but I have multiple headers and in the excel file the first row has an extra cell on the left (see attached image).
How can I solve the problem?

Thanks.

3 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 18 May 2012, 11:28 AM
Hi Deborah,

You can handle the ElementExporting event and set the missing values there .
Let me know in case you need further assistance in that.

Kind regards,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Deborah
Top achievements
Rank 1
answered on 18 May 2012, 01:17 PM
Hi Pavel,
I don't want to show the missing values, I want the first row of headers is displayed properly.
(the first cell on the left shouldn't exist).
Thanks,

Deborah
0
Deborah
Top achievements
Rank 1
answered on 18 May 2012, 02:01 PM
For now I solved this way (I cancel the export of the first blank header):

bool firstTime = true
 
private void gridxyz_ElementExporting(object sender, Telerik.Windows.Controls.GridViewElementExportingEventArgs e)
        {
            if (e.Element == ExportElement.HeaderRow || e.Element == ExportElement.CommonColumnHeader)
            {
                if (e.Value == "" && firstTime)
                {
                    e.Cancel = true;
                    firstTime = false;
                }
                else
                {
                    e.Background = Colors.LightGray;
                    e.FontWeight = FontWeights.Bold;
                    e.Height = 38;
                    e.VerticalAlignment = VerticalAlignment.Center;
                }
            }

Deborah
Tags
GridView
Asked by
Deborah
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Deborah
Top achievements
Rank 1
Share this question
or