Export Excel with Updated Column Value

3 posts, 1 answers
  1. Joe
    Joe avatar
    69 posts
    Member since:
    Dec 2007

    Posted 02 Jun 2009 Link to this post

    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">  
                           <telerik:GridGroupByField FieldAlias="Activity" FieldName="daterounded" FormatString="{0:D}" HeaderValueSeparator=" from date: "></telerik:GridGroupByField> 
                           <telerik:GridGroupByField FieldName="daterounded" SortOrder="Descending"></telerik:GridGroupByField> 
                        <telerik:GridSortExpression FieldName="dateadded" SortOrder="Descending" /> 
                        <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" /> 


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

    Any ideas?
  2. Answer
    Yavor avatar
    11 posts

    Posted 05 Jun 2009 Link to this post

    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,
    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.
  3. Joe
    Joe avatar
    69 posts
    Member since:
    Dec 2007

    Posted 05 Jun 2009 Link to this post

    That makes sense. I'll give it a go...
    Thanks Yavor,
Back to Top