Exporting grid to excel

4 posts, 1 answers
  1. Nick
    Nick avatar
    7 posts
    Member since:
    Oct 2010

    Posted 01 Dec 2017 Link to this post

    I've created a datatable using the following query:
    select str(-abs(100),10,2) as crash
    (a negative number cast to string)

    When I use this datatable in a grid and export it to excel, I'll get the folowwing error:
    We found a problem with this formula. Try clicking Insert Function on the Formulas tab to fix it.\n\nNot trying to type a formula? When the first character is an equal (=) or minus (-) sign, RadSpreadsheet thinks it is a formula. For example, when you type =1+1 the cell shows 2.

    I've implemented the workarround in https://www.telerik.com/forums/output-to-excel-error
    (The cell has the Format attribute set to "@")
    This works for regular strings starting with a dash, but not working for the specific situation described before.
  2. Answer
    Attila Antal
    Admin
    Attila Antal avatar
    523 posts

    Posted 06 Dec 2017 Link to this post

    Hi Nick,

    Setting the DataFormatString property of GridColumn to "{0:@}" will produce the required behavior.
    <telerik:GridBoundColumn ... DataFormatString="{0:@}"></telerik:GridBoundColumn>

    Please check out the sample I've attached that demonstrates this scenario, to see if that works for you.

    Kind Regards,
    Attila Antal
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Nick
    Nick avatar
    7 posts
    Member since:
    Oct 2010

    Posted 07 Dec 2017 in reply to Attila Antal Link to this post

    Hi Attila,

    Thanks, this solved the problem. 

    Below my solution using the GridColumnCreated event

    protected void ColumnCreated(object sender, GridColumnCreatedEventArgs e)
    {
        if (e.Column is GridBoundColumn boundColumn)
        {
            if (boundColumn.DataType == typeof(string))
                boundColumn.DataFormatString = "{0:@}";
        }
    }
  4. Attila Antal
    Admin
    Attila Antal avatar
    523 posts

    Posted 07 Dec 2017 Link to this post

    Hi Nick,

    I am glad to hear the problem has been solved!

    Also, I would like to thank you for sharing your method with the community, well done!

    Kind Regards,
    Attila Antal
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top