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

Export Excel with Updated Column Value

2 Answers 189 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 2
Joe asked on 02 Jun 2009, 06:28 PM
Hi,
I'm trying to export a grouped grid to excel. Only one issue left...

In RadGrid1_ItemDataBound I change the value of a column "totalseconds" from a number (92) to a string (1:32). Basically I'm converting seconds to a minute string. When I export to Excel, the column "totalseconds" spits out a bunch of zeros, instead of the updated value. The zeroes are not even the correct "original" values.

I think the question is... how do I maintain the updated value, from ItemDataBound, when exporting to Excel? I tried changing the value in ItemCreated and PreRender, but that didn't seem to work either. I have also tried everything in "Exporting Tips and Tricks" to no avail.

Here's the relevant code:

 

<telerik:radgrid AllowSorting="true" AutoGenerateColumns="false" AllowFilteringByColumn="False" 
            AllowPaging="true" PageSize="20" id="RadGrid1" Skin="Gray" runat="server" OnExcelExportCellFormatting="RadGrid1_ExcelExportCellFormatting">  
            <MasterTableView> 
               <GroupByExpressions> 
                 <telerik:GridGroupByExpression> 
                   <SelectFields> 
                       <telerik:GridGroupByField FieldAlias="Activity" FieldName="daterounded" FormatString="{0:D}" HeaderValueSeparator=" from date: "></telerik:GridGroupByField> 
                   </SelectFields> 
                   <GroupByFields> 
                       <telerik:GridGroupByField FieldName="daterounded" SortOrder="Descending"></telerik:GridGroupByField> 
                   </GroupByFields> 
                 </telerik:GridGroupByExpression> 
                </GroupByExpressions> 
                <SortExpressions> 
                    <telerik:GridSortExpression FieldName="dateadded" SortOrder="Descending" /> 
                </SortExpressions> 
                <Columns> 
                    <telerik:GridBoundColumn DataField="dateadded" UniqueName="dateadded" HeaderText="Activity Date (EST)" /> 
                    <telerik:GridBoundColumn DataField="title" UniqueName="title" HeaderText="Lesson Title" /> 
                    <telerik:GridBoundColumn DataField="status" UniqueName="status" HeaderText="Lesson Status" /> 
                    <telerik:GridBoundColumn DataField="totalseconds" UniqueName="totalseconds" HeaderText="Total Time" /> 
                </Columns> 
            </MasterTableView> 
</telerik:radgrid> 

 

Private Sub RadGrid1_NeedDataSource(ByVal [source] As ObjectByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs)_ Handles RadGrid1.NeedDataSource  
        ' Populate RadGrid from DB  
End Sub 
 
Private Sub RadGrid1_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound  
        If TypeOf e.Item Is GridDataItem Then 
            Dim item As GridDataItem = e.Item  
            Dim totalseconds As Integer = CInt(item("totalseconds").Text)  
            Dim totaltime As String = formatTime(totalseconds) 'formatTime returns a string  
            item("totalseconds").Text = totaltime  
        End If 
End Sub 
 
Protected Sub btnExport_Click(ByVal sender As ObjectByVal e As System.Web.UI.ImageClickEventArgs) Handles btnExport.Click  
        RadGrid1.ExportSettings.ExportOnlyData = True 
        RadGrid1.ExportSettings.IgnorePaging = True 
        RadGrid1.ExportSettings.OpenInNewWindow = True 
        RadGrid1.MasterTableView.ExportToExcel()  
End Sub 
 

Any ideas?
Thanks,
Joe

2 Answers, 1 is accepted

Sort by
0
Accepted
Yavor
Telerik team
answered on 05 Jun 2009, 09:57 AM
Hi Joe,

When exporting, the grid control will export the data from the underlying datasource.
Having this in mind, one possible option in this case, would be to use the NeedDataSource event handler, and pass a temporary datasource, which contains the formatted data. This way, it will be reflected when exported under excel.
I hope this suggestion helps.

All the best,
Yavor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Joe
Top achievements
Rank 2
answered on 05 Jun 2009, 04:22 PM
That makes sense. I'll give it a go...
Thanks Yavor,
Joe
Tags
Grid
Asked by
Joe
Top achievements
Rank 2
Answers by
Yavor
Telerik team
Joe
Top achievements
Rank 2
Share this question
or