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

RadGridView export with ToExcelML, number are string

8 Answers 185 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Castgroup srl
Top achievements
Rank 2
Castgroup srl asked on 27 Jan 2011, 05:08 PM
I 'm exporting data from radgridview in Silverlight 3 with ToExcelML function.
The code xaml for the column is:

 

 

<tlkEx:RadGridViewEx x:Name="gvData" Grid.Row="1" AutoGenerateColumns="False" IsReadOnly="True" ColumnsWidthMode="Auto">
<tlkGrid:RadGridView.Columns> ....
<tlkGrid:GridViewDataColumn UniqueName="LetturaDataCalcolo" HeaderText="sospesoEFLetturaDataCalcolo" DataMemberBinding="{Binding LetturaDataCalcolo}" DataFormatString="{}{0:#,0.######}" TextAlignment="Right"/> ...
</tlkGrid:RadGridView.Columns></tlkEx:RadGridViewEx>

 

 

 

Code behind to generate Excel:
if (gvData != null)
{
var clientStatusColumn = GridView.Columns.OfType<GridViewColumn>().Where(c => c.UniqueName.Equals(Constants.GridViewColumnsIDs.clientStatusNotifyID, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
if (clientStatusColumn != null) clientStatusColumn.IsVisible = false;
String content = GridView.ToExcelML(true);
if (clientStatusColumn != null) clientStatusColumn.IsVisible = true;
if (!String.IsNullOrEmpty(content))
{
Uri uri = new Uri(HtmlPage.Document.DocumentUri, String.Format("ExportHandler.aspx?type=xls&uid={0}", App.UserInfo.UserName));
WebClient client = new WebClient();
client.UploadStringCompleted += new UploadStringCompletedEventHandler(exportClientUploadStringCompleted);
client.UploadStringAsync(uri, content);
}
}

The export of number column as the LetturaDataCalcolo in excel is a text cell or has at the beginning a char like '; It's not a number cell even if I specify, in the inizialize method,:
((GridViewDataColumn)gvData.Columns["LetturaDataCalcolo"]).DataType = typeof(Double);
The item source of the grid is a list of type that have the correct decimal type

 

 

Is it possible, in Silverlight3 with ToExcelML, format the cell in the output excel file like a number ?
I can send the excel generated that I cannot attach here.

Regards
Vittorio

8 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 31 Jan 2011, 08:20 AM
Hello,

 Since you have defined DataFormatString for this column the grid will call string.Format() with this format and you will get new string type value.

Kind regards,
Vlad
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Charlie
Top achievements
Rank 1
answered on 29 Mar 2012, 06:22 PM
Hey,

Is it possible to convert that column into a double even if there is a data format string applied to that column?

Best regards,

Charlie
0
Nick
Telerik team
answered on 30 Mar 2012, 09:18 AM
Hi Charlie,

You can use the ElementExporting event of the grid to modify the exported value any way you want to. You can see how this can be done in our online examples here

Hope this helps! 

All the best,
Nik
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Charlie
Top achievements
Rank 1
answered on 09 Apr 2012, 07:14 PM
Hey Telerik,

I have a RadGridView and it is grouped by a column. Also, I've implemented aggregate functions to the grid (Sum function). When I export it, I've dealt with the problem where all of the exported cells in the tables are strings (even though they are numbers). I implemented that using this link you've provided, and thanks a lot! http://demos.telerik.com/silverlight/#GridView/Exporting
Unfortunately, I am dealing with exporting the GroupFooterCell. The exported results (the aggregate results) are always string. Even if it's just a number. Here is the code I'm using:

else if (e.Element == ExportElement.GroupFooterCell)
                {
                    GridViewDataColumn column = e.Context as GridViewDataColumn;
                    QueryableCollectionViewGroup qcvGroup = e.Value as QueryableCollectionViewGroup;

                    if (column != null && qcvGroup != null && column.AggregateFunctions.Count > 0)
                    {
                        string value = GetAggregates(qcvGroup, column);
                        value = ReplaceIllegalExcelCharacters(value);
                        double Number;
                        bool IsNum = double.TryParse(value, out Number);
                        if (IsNum)
                            e.Value = Number;
                        else
                            e.Value = value;
                    }
                }

This code runs, and the e.Value is being set to the correct value, but it's string. I've tried setting a break point at e.Value = Number, and that breakpoint is being hit. I've even tried hardcoding that e.Value = 10, and the result appears in the exported excel sheet, but it's a string. I'm not sure what's going on! Thanks!

Best regards,

Charlie
0
Dimitrina
Telerik team
answered on 10 Apr 2012, 09:49 AM
Hi,

 This happens as the original value in e.Value is not of type Number. The Element (for the GroupFooterCell) is already created by the time when you change the value.

To change the exported data, you can extract the text from the resulted Stream and change the type for those nodes to be Number instead of String.

All the best,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Charlie
Top achievements
Rank 1
answered on 10 Apr 2012, 04:25 PM
Thanks for the reply Didie,

I'm not sure what you mean by "To change the exported data, you can extract the text from the resulted Stream and change the type for those nodes to be Number instead of String."

Best regards,

Charlie
0
Charlie
Top achievements
Rank 1
answered on 10 Apr 2012, 04:32 PM
Hey Didie,

When I set a breakpoint and view the dataType of "e.Context as GridViewDataColumn", I get a result of Double. I'm not sure what to do from here. Thanks for your help!

Best regards,

Charlie
0
Dimitrina
Telerik team
answered on 12 Apr 2012, 09:38 AM
Hello,

 You can take the generated stream and proceed with it as you would like. For example:

this.clubsGrid.Export(tempStream, exportOptions);
                    var reader = newStreamReader(tempStream);
                    tempStream.Position = 0;
                    var text = reader.ReadToEnd();
  
                    // change the text
                    var writer = new StreamWriter(stream);
                    writer.Write(text);
                    writer.Flush();

Kind regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Castgroup srl
Top achievements
Rank 2
Answers by
Vlad
Telerik team
Charlie
Top achievements
Rank 1
Nick
Telerik team
Dimitrina
Telerik team
Share this question
or