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

How to export the RadGridView with number columns?

12 Answers 127 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Yuxiao
Top achievements
Rank 1
Yuxiao asked on 07 Nov 2014, 01:09 AM
Hi, 

I tried to export the RadGridView to an Excel file, however, every column there seems to be string. How to export the RadGridView with number columns?

I tried to set the columns as double but not work,

    ((GridViewBoundColumnBase)grid.Columns[17]).DataType = typeof(double);

I also tried following and not work either,

    ((GridViewDataColumn)grid.Columns[17]).DataFormatString = "0.00";

The XML in the exported file is something like 

<Row>
    <Cell><Data ss:Type="String">Title</Data></Cell>
    <Cell><Data ss:Type="String">SE</Data></Cell>
    <Cell><Data ss:Type="String">Hour</Data></Cell>
</Row>

While the right format should be 

    <Cell><Data ss:Type="Number">Hour</Data></Cell>

To bind the fields one by one maybe an option, but I just want it to simple. 

Could anyone help? Thank you very much in advance!

Here are my code in the cs.

  private void Button_Export_Click_1(object sender, RoutedEventArgs e)
        {
            RadGridView grid = Grid_Records;
            ExportFormat format = ExportFormat.ExcelML;
            string extension = "xls";
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.DefaultExt = extension;
            dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, "xls");
            dialog.FilterIndex = 1;

            if (dialog.ShowDialog() == true)
            {
                using (Stream stream = dialog.OpenFile())
                {
                    GridViewExportOptions exportOptions = new GridViewExportOptions();
                    exportOptions.Format = format;
                    exportOptions.ShowColumnFooters = true;
                    exportOptions.ShowColumnHeaders = true;
                    exportOptions.ShowGroupFooters = true;

                    ((GridViewBoundColumnBase)grid.Columns[17]).DataType = typeof(double);

                    grid.Export(stream, exportOptions);
                }
            }
        }


12 Answers, 1 is accepted

Sort by
0
Yuxiao
Top achievements
Rank 1
answered on 07 Nov 2014, 01:14 AM
By the way, in the xaml file, the code is simple,

<telerik:RadGridView x:Name="Grid_Records" AutoGenerateColumns="False" ShowInsertRow="False" RowIndicatorVisibility="Collapsed" ShowGroupPanel="False" IsReadOnly="True" Height="800" Width="1200" Margin="0,20,0,0" HorizontalAlignment="Left" SelectionMode="Extended" ClipboardCopyMode="All" >
</telerik:RadGridView>
0
Dimitrina
Telerik team
answered on 07 Nov 2014, 12:39 PM
Hi,

You can subscribe for the ElementExporting event of RadGridView and parse the value to be exported to the desired format and set it as e.Value. You can also check our online documentation on Exporting.

Regards,
Dimitrina
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Yuxiao
Top achievements
Rank 1
answered on 09 Nov 2014, 12:20 PM
Got it, thank you very much for the quick help !

I know how to do it now. And I am real sorry to post stupid questions like this, just don't have much time to search.

But here is one more, is the comments for the header cell needs to be done in the same way (using elementexporting)?

I added the column head in the following way, and would like to a have comment in the head cell 'stay'

 GridViewDataColumn stayColumn = new GridViewDataColumn()
                                                {
                                                    UniqueName = "stay",
                                                    Header = "stay",
                                                };
            Grid_Records.Columns.Add(stayColumn);


0
Dimitrina
Telerik team
answered on 10 Nov 2014, 10:16 AM
Hi,

The Header ("stay") should be exported without any additional code, is it not the case?
You can also check the Export Custom Header troubleshooting article in our online documentation.

Regards,
Dimitrina
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Yuxiao
Top achievements
Rank 1
answered on 10 Nov 2014, 10:28 AM
Yes, without any other codes. Thanks.
0
Dimitrina
Telerik team
answered on 10 Nov 2014, 03:28 PM
Hi,

I am bit confused whether you have a problem in the default case or not. If the header is not exported (which should be working by default), then may I ask you to open a new support thread and send us a demo project illustrating the problematic behavior?

Regards,
Dimitrina
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Yuxiao
Top achievements
Rank 1
answered on 11 Nov 2014, 05:45 AM
The header is exported without any problem. I just would like to have a comment in the header cell.
0
Dimitrina
Telerik team
answered on 11 Nov 2014, 08:19 AM
Hello,

Basically, the way to modify the values to be exported is subscribing for the ElementExporting event of RadGridView and setting the value to be exported (e.Value) as you wish.
For example:
private void clubsGrid_ElementExporting(object sender, GridViewElementExportingEventArgs e)
{
    if (e.Element == ExportElement.HeaderCell)
    {
        var column = e.Context as GridViewDataColumn;
        if (column != null)
        {
            e.Value = "any value you wish to export";
        }
    }
}

If you would like to export an additional row, then you can subscribe for ElementExported event. This is also illustrated on our online demo on Exporting RowDetails

Regards,
Dimitrina
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Yuxiao
Top achievements
Rank 1
answered on 11 Nov 2014, 10:46 AM
I have no problem with the value itself, I have the following problems,

1. How to format the value into a percentage format? Have a % in the cell for a numeric value.
2. Would like to add a comment to the head row to put some comments.

Thanks in advance!

  private void Grid_Records_ElementExporting(object sender, GridViewElementExportingEventArgs e)
        {
            if (e.Element == ExportElement.Cell)
            {
                if ((e.Context as GridViewDataColumn).Header.ToString().Contains("Avg over stay"))
                {
                    decimal oUsage;
                    try
                    {
                        var remove = decimal.TryParse(e.Value.ToString(), out oUsage);
                        if (remove)
                        {
                                    e.Value = oUsage;
                        }
                    }
                    catch(Exception exporterror)
                    {
                    }
                }}}

0
Dimitrina
Telerik team
answered on 13 Nov 2014, 11:23 AM
Hello,

In order to format the values, you can do so on column level setting DataFormatString of your column.
For example:

<telerik:GridViewDataColumn DataMemberBinding="{Binding StadiumCapacity}" DataFormatString="{}{0:#\%}"/>

The formatted value will be exported to Excel.

As to your second question, RadGridView's export does not suggest a way of specifying custom comments to the exported data. The way to modify the values to be exported is subscribing for the ElementExporting and ElementExported events of RadGridView.

Regards,
Dimitrina
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Kamran
Top achievements
Rank 1
answered on 05 Dec 2017, 01:16 PM

Hi,

 

i know this thread is kinda old, but I found it and it fits my request quite well.

I got a RadTreeListView and would like to export the content to excel. I found the documentation but I am not sure what to put instead of "gridViewExport" in the example

https://docs.telerik.com/devtools/wpf/controls/radgridview/export/export

 

I didn't write any of the code, neither have I worked with Telerik. I just want to export the table as I see it to Excel

 

Thanks in advance

0
Vladimir Stoyanov
Telerik team
answered on 07 Dec 2017, 01:21 PM
Hello Kamran,

The "gridViewExport" in that case is value of the Name property of the RadGridView. So if you want to use the same example, you need to set the Name of the RadTreeListView in Xaml like so:
<telerik:RadTreeListView x:Name="gridViewExport" />

I hope this helps. Let me know if I can be of any further assistance.

Regards,
Vladimir Stoyanov
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
GridView
Asked by
Yuxiao
Top achievements
Rank 1
Answers by
Yuxiao
Top achievements
Rank 1
Dimitrina
Telerik team
Kamran
Top achievements
Rank 1
Vladimir Stoyanov
Telerik team
Share this question
or