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

2013Q1 RadGridView Export question

3 Answers 156 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ricky
Top achievements
Rank 1
Ricky asked on 18 May 2013, 12:49 AM
Hi there,

my problem is I have a grid view, and I have tons of login in xaml which perform the grouping and aggregate function, some of the group header/footer may have a DataTemplate to host the data. When I export the grid using the native Export function, I have the following issue:

1. I lost the Group Header, the column I am used to group is not shown in UI and I disallow use to change the grouping
2. I lost the ColumnGroups
3. Some of the cell is incorrect datatype (e.g. number and was interpreted as date in Excel)
4. All the template data cannot be displayed, and what's generated is the datatype instead of the string value on the grid (same as copy from the grid directly using Ctrl-C)

Any help will be appreciated!

Here are some examples of the xaml:

1. Grid Definition
<telerik:RadGridView Name="GridViewMyGrid"                                          
     ItemsSource="{Binding MyCollection}" 
     AutoGenerateColumns="False" 
     IsReadOnly="True"
     CanUserFreezeColumns="False"
     GroupRenderMode="Flat"
     ShowColumnFooters="True"
     ShowGroupFooters="True"
     AutoExpandGroups="True"
     IsFilteringAllowed="False"
     ShowGroupPanel="False" 
     FrozenColumnCount="2" 
     SelectionMode="Extended" 
     >

2. Column Group
<telerik:RadGridView.ColumnGroups>
    <telerik:GridViewColumnGroup  Name="Group1ColumnGroup" 
                            Header="Group1">
        <telerik:GridViewColumnGroup.HeaderTemplate>
            <DataTemplate>
                <TextBlock Text="Group1" 
                        TextAlignment="Center" 
                        FontWeight="Bold"></TextBlock>
            </DataTemplate>
        </telerik:GridViewColumnGroup.HeaderTemplate>
    </telerik:GridViewColumnGroup>


3. Column (I have tons of column and just show two here, the 1st is used for grouping)
<telerik:RadGridView.Columns>                            
    <telerik:GridViewDataColumn DataMemberBinding="{Binding GroupName}" ShowColumnWhenGrouped="False" />
    <telerik:GridViewDataColumn Header="Item1"
                                DataMemberBinding="{Binding Item1}">                            
        <telerik:GridViewDataColumn.AggregateFunctions>
            <telerik:FirstFunction/>
        </telerik:GridViewDataColumn.AggregateFunctions>
        <telerik:GridViewDataColumn.GroupFooterTemplate>
            <DataTemplate>
                <TextBlock FontWeight="Bold">
                    <Run Text="Subtotal "/>
                    <Run Text="{Binding Value.GroupName}"/>
                </TextBlock>                                        
            </DataTemplate>
        </telerik:GridViewDataColumn.GroupFooterTemplate>
        <telerik:GridViewColumn.Footer>
            <TextBlock FontWeight="Bold">
                <Run Text="Asset Total"/>
            </TextBlock>
        </telerik:GridViewColumn.Footer>
    </telerik:GridViewDataColumn>


And here is the method in the code behind to export:

 

private void ButtonExportStrats_OnClick(object sender, RoutedEventArgs e)
{
    var grid = GridViewMyGrid;            
    var dialog = new SaveFileDialog();
    dialog.DefaultExt = "*.xlsx";
    dialog.Filter = "Excel Workbook (*.xlsx)|*.xlsx";
    if (dialog.ShowDialog() == true)
    {
        Workbook book = null;
        using (var stream = new MemoryStream())
        {
            grid.Export(stream, new GridViewExportOptions()
            {
                Format = ExportFormat.Csv,
                ShowColumnFooters = grid.ShowColumnFooters,
                ShowColumnHeaders = grid.ShowColumnHeaders,
                ShowGroupFooters = grid.ShowGroupFooters
            });
            stream.Position = 0;
            book = new CsvFormatProvider().Import(stream);
            if (book != null)
            {
                var provider = new XlsxFormatProvider();
                using (var output = dialog.OpenFile())
                {
                    provider.Export(book, output);
                }
            }
        }
    }
}

3 Answers, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 20 May 2013, 12:28 PM
Hi Ricky,

You could check this online demo for an example on how you could export the GridView as it is displayed.

All the best,
Yoan
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Ricky
Top achievements
Rank 1
answered on 20 May 2013, 05:27 PM
my code was from that example, and I don't see the result is same as the grid, the export code is simply this in the example:

var exportOptions = new GridViewExportOptions();
exportOptions.Format = format;
exportOptions.ShowColumnFooters = true;
exportOptions.ShowColumnHeaders = true;
exportOptions.ShowGroupFooters = true;
grid.Export(stream, exportOptions);

which is the same as what I have.
0
Yoan
Telerik team
answered on 23 May 2013, 01:45 PM
Hi Ricky,

Could you try isolating the problems in a sample project and send it to us? We will debug it on our side and will assist you further. You can check this blog post for a reference, which shows you how to isolate a problem in a sample project.

Thank you in advance.

Regards,
Yoan
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
Ricky
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Ricky
Top achievements
Rank 1
Share this question
or