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

RadGrid Export to Excel Issue

1 Answer 60 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jon
Top achievements
Rank 1
Jon asked on 01 Oct 2012, 09:25 PM
I have a RadGrid that works just fine except for one small issue when exporting to excel. I have a template column that uses OnItemCreated to custom format that column.   Debugging through the code it seems that during the OnItemCreated event the e.Item.DataItem is null during export but during normal usage of the grid this value is there.  Perhaps I am doing this completely backward but I think I'm on the right track but may just be missing some small property change or something.  The template column on export has no data while the grid in the browser does.  See attached screenshots.  Here's my code:

<telerik:RadGrid runat="server"
                 ID="RadGrid_CompletedTrainingReport"
                 OnNeedDataSource="RadGrid_CompletedTrainingReport_NeedDataSource"
                 OnDetailTableDataBind="RadGrid_CompletedTrainingReport_OnDetailTableDataBind"
                 OnItemCreated="RadGrid_CompletedTrainingReport_OnItemCreated"
                 ExportSettings-ExportOnlyData="true"
                 ExportSettings-Excel-Format="Html"
                 AllowSorting="true"
                 EnableHeaderContextMenu="true"
                 OnCustomAggregate="RadGrid_CompletedTrainingReport_OnCustomAggregate">
    <MasterTableView DataKeyNames="TrainingTrackingID"
                     Name="TrainingTrackings"
                     AutoGenerateColumns="false"
                     CommandItemDisplay="TopAndBottom"
                     CommandItemSettings-ShowExportToExcelButton="true"
                     CommandItemSettings-ShowAddNewRecordButton="false"
                     ExpandCollapseColumn-Visible="true">
        <Columns>
            <telerik:GridBoundColumn DataField="StudentFullname"
                                     HeaderText="Student Name"
                                     SortExpression="StudentLastname">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="TrainingName"
                                     HeaderText="Training Name">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ILACode"
                                     HeaderText="ILA Code">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="CompletedDate"
                                     HeaderText="Completion Date">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn HeaderText="Certificate Number">
                <ItemTemplate>
                    <asp:ImageButton ID="ImageButton_PrintCertificate"
                                     ImageUrl="~/images/Certificate.png"
                                     runat="server"
                                     AlternateText="Print Certificate"/>
                    <asp:Label Text='<%# Eval("CertificationNumber") %>'
                               runat="server"
                               ID="Label_CertificateNumber"/>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridBoundColumn DataField="CEHour"
                                     HeaderText="CE Hour">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="SimHour"
                                     HeaderText="Sim Hour">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="StandHour"
                                     HeaderText="Stand Hour">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="EOHour"
                                     HeaderText="EO Hour"
                                     Display="false">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="PJMHour"
                                     HeaderText="PJM Hour"
                                     Display="false">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ProfHour"
                                     HeaderText="Prof Hour"
                                     Display="false">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="SOCCEDUploadMessage"
                                     HeaderText="SOCCED Message"
                                     HeaderStyle-Width="10%">
            </telerik:GridBoundColumn>
        </Columns>
        <DetailTables>
            <telerik:GridTableView DataKeyNames="ID"
                                   Name="LessonTrackings"
                                   AutoGenerateColumns="false"
                                   ShowFooter="true">
                <Columns>
                    <telerik:GridBoundColumn DataField="Name"
                                             HeaderText="Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Type"
                                             HeaderText="Type">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Status"
                                             HeaderText="Status">
                    </telerik:GridBoundColumn>
                    <telerik:GridTemplateColumn HeaderText="Score">
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink_Score"
                                           CssClass="HyperLink_Score_Class"
                                           runat="server"
                                           Text='<%# Eval("Score") %>'/>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn DataField="SecondsOn"
                                                HeaderText="Time Spent On (hh:mm:ss)"
                                                Aggregate="Custom"
                                                FooterText="Total Time: ">
                        <ItemTemplate>
                            <asp:Label runat="server"
                                       ID="Label_SecondsOn"/>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </telerik:GridTableView>
        </DetailTables>
    </MasterTableView>
</telerik:RadGrid>
protected void RadGrid_CompletedTrainingReport_OnItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem gdi = (GridDataItem)e.Item;
 
        if (gdi.OwnerTableView.Name == "Registrations")
        {
            if (gdi.DataItem is spTrainingStatusReport_Result)
            {
                var spTrainingStatusReport_Result = (spTrainingStatusReport_Result)gdi.DataItem;
                var Label_SecondsOn = (Label)gdi.FindControl("Label_SecondsOn");
                var SecondsOnTimespan = new TimeSpan(0, 0, spTrainingStatusReport_Result.SecondsOnTraining);
 
                Label_SecondsOn.Text = SecondsOnTimespan.ToString("c");
            }
        }
    }
}

Thanks for any help!

1 Answer, 1 is accepted

Sort by
0
Jon
Top achievements
Rank 1
answered on 03 Oct 2012, 08:08 PM
I went another route and got this fixed.
Tags
Grid
Asked by
Jon
Top achievements
Rank 1
Answers by
Jon
Top achievements
Rank 1
Share this question
or