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

Datatype conversion in export to excel method

6 Answers 98 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Eli
Top achievements
Rank 1
Eli asked on 10 May 2012, 08:44 PM

Hello,

I am using Telerik version 2012.1.326
I have a RadGridView, and I am using the ToExcelML() method provided (see code below). The problem is that all of the data is converted into strings by the method. The data comes out of a datatable as strings, decimals, and datetimes. And it is shown in the grid view as it's given datatype. You can filter the columns, and the filter also respects datatype. How do I keep the data as it's original datatype when it is exported?

Thank you,
Eli


            SaveFileDialog dialog = new SaveFileDialog();
            string extension = "xml";
            dialog.InitialDirectory = Settings.Default.DataSavePath;
            dialog.DefaultExt = extension;
            dialog.Filter = string.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, "ExcelML");
            dialog.FilterIndex = 1;

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                var gridOutput = this.radGridView.ToExcelML().Replace("Worksheet1", "Output");

                using (Stream stream = dialog.OpenFile())
                {
                    byte[] bytes = Encoding.UTF8.GetBytes(gridOutput);
                    stream.Write(bytes, 0, bytes.Length);
                }

6 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 11 May 2012, 07:03 AM
Hello,

 Do you have any string formats defined for your columns? 

Kind regards,
Vlad
the Telerik team

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

0
Eli
Top achievements
Rank 1
answered on 11 May 2012, 02:18 PM
Hi Vlad,

Thank you for the quick reply. I don't have string formats, because the data is not of string type.

Eli
0
Vlad
Telerik team
answered on 11 May 2012, 02:21 PM
Hello,

 String formats are for all other types except strings. Do you have anything set for column DataFormatString and/or DataMemberBinding StringFormat? 

Kind regards,
Vlad
the Telerik team

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

0
Eli
Top achievements
Rank 1
answered on 11 May 2012, 02:44 PM
Vlad,

DataFormatString is set by a style, we have an attached property because the columns are added in the program.
 
                                  <attached:ColumnStyle StyleName="Decimal3">
                                        <attached:ColumnStyle.Style>
                                            <Style TargetType="{x:Type telerik:GridViewDataColumn}">
                                                <Setter Property="CellStyle"
                                                        Value="{StaticResource DecimalCellTemplate}" />
                                                <Setter Property="DataFormatString"
                                                        Value="{}{0:n3}" />
                                            </Style>
                                        </attached:ColumnStyle.Style>
                                    </attached:ColumnStyle>
0
Vlad
Telerik team
answered on 14 May 2012, 07:48 AM
Hi,

The grid export will use the defined string format to convert your data to string with the specified format. 

All the best,
Vlad
the Telerik team

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

0
Eli
Top achievements
Rank 1
answered on 14 May 2012, 06:43 PM
Vlad,
The RadGridView is displaying the data in the format that we set in the template. The filter is responding correctly as well. The problem seems to be in the export to excel method. I set a breakpoint on the following code and looked at the datatype of the gridViewDataColumn, and it displayed as System.Nullable`1[System.Decimal] I do not know if the fact that it is nullable is forcing a conversion to string, or something else is happening, but the export to excel method is not picking up the datatype correctly.

Thanks,
Eli
            

                            GridViewDataColumn gridViewDataColumn = e.Column as GridViewDataColumn;

                            Binding isVisibleBinding = new Binding("IsVisible");
                            isVisibleBinding.Mode = BindingMode.TwoWay;
                            isVisibleBinding.Source = templateColumn;

                            gridViewDataColumn.SetBinding(GridViewDataColumn.IsVisibleProperty, isVisibleBinding);
Tags
GridView
Asked by
Eli
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Eli
Top achievements
Rank 1
Share this question
or