Custom column not exported to excel

4 posts, 0 answers
  1. Deborah
    Deborah avatar
    31 posts
    Member since:
    Jun 2011

    Posted 18 May 2012 Link to this post

    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.
  2. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 18 May 2012 Link to this post

    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 >>

  3. Deborah
    Deborah avatar
    31 posts
    Member since:
    Jun 2011

    Posted 18 May 2012 Link to this post

    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
  4. Deborah
    Deborah avatar
    31 posts
    Member since:
    Jun 2011

    Posted 18 May 2012 Link to this post

    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
Back to Top