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

Export example not working.

17 Answers 227 Views
GridView
This is a migrated thread and some comments may be shown as answers.
William
Top achievements
Rank 1
William asked on 27 Jan 2017, 05:14 PM

I tried to implement the Export functionality for GridView shown in the UI for WPF example (Word, Excel, CSV export), and I get everything to work except that the "context" is incorrect. When I get into the code, the object that is passed as a parameter is the high-level parent of the GridView I would like to export. What am I doing wrong that I can't get the "context" parameter to be correct?

XAML:

    xmlns:exp="clr-namespace:GridView.Exporting"
    <UserControl.Resources>
        <exp:ExportingModel x:Key="context" />

 

    <telerik:RadButton x:Name="ExpGPData" Content="Export" Grid.Column="0" ToolTip="Export this data to an Excel format" Command="{Binding ExportCommand, Source={StaticResource context}}" CommandParameter="{Binding}"/>

 

ExportingModel.cs is same as in the example.

17 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 01 Feb 2017, 11:57 AM
Hi William,

I tested the referred demo locally, and it seems to be working as expected. May I kindly ask you to shed some light on the context not being correct? Is the ExportCommand in the ExportingModel triggered on your end?

Thank you in advance for your cooperation.

Regards,
Stefan X1
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
William
Top achievements
Rank 1
answered on 01 Feb 2017, 06:15 PM

The code was added EXACTLY as in the WPF demos.  This is the button that exports:

<telerik:RadButton x:Name="ExpGapAnalysis" Content="Export" Grid.Row="1" Grid.Column="6" ToolTip="This will export the Gap Analysis" Command="{Binding ExportCommand, Source={StaticResource context}}" CommandParameter="{Binding}" />

I can trace down to the statement: public void Execute(object parameter) in the class: ExportCommand.

The parameter is set to the class for the entire form, not for the particular grid where the button is located.

 

0
Stefan
Telerik team
answered on 06 Feb 2017, 09:00 AM
Hello William,

Though I am not completely sure what the exact cause for such behavior might be, may I ask you to try binding the CommandParameter through ElementName?
<telerik:RadButton Content="Export"
                               Command="{Binding ExportCommand, Source={StaticResource context}}"
                               CommandParameter="{Binding ElementName=RadGridView1}"
                               Margin="0,10,0,0" />

Is the grid now passed correctly as a parameter?

Regards,
Stefan X1
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
William
Top achievements
Rank 1
answered on 06 Feb 2017, 02:42 PM

That worked to get the export functionality working. However, are you aware that the export doesn't work completely? With "SelectedExportFormat"="Excel", the headers come out without names:

 

0
William
Top achievements
Rank 1
answered on 06 Feb 2017, 02:43 PM
The second type of export (SelectedExportFormat="ExcelML") doesn't have the correct headers, either:
0
Stefan
Telerik team
answered on 09 Feb 2017, 11:21 AM
Hello William,

Thank you for the provided images.

Generally speaking, such behavior is replicated when custom Header is defined for the column. Can you please clarify whether you have applied a custom Header content? If this is the case, please, take a look at the Export Custom Header topic.

Hopefully, it helps.

Regards,
Stefan X1
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
William
Top achievements
Rank 1
answered on 09 Feb 2017, 01:42 PM
Yes, there are custom headers. I am using your example "GridView.Exporting". Where do I put the code for the header?
0
William
Top achievements
Rank 1
answered on 09 Feb 2017, 09:25 PM
I was able to get the header cells to display correctly. How do I get the actual raw value of the data rather than the formatted value (e.Value in method ElementExporting)?
0
Stefan
Telerik team
answered on 14 Feb 2017, 10:46 AM
Hi William,

A possible solution for this would be to access the Header of the needed column. This can be achieved by utilizing the Columns collection of RadGridView. Would such an approach be feasible for you?

Another approach would be to use the UniqueName property of the needed column, as its value will be corresponding to the DataMemberBinding of the column.

I hope you find one of these suggestions useful.

Regards,
Stefan X1
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
William
Top achievements
Rank 1
answered on 14 Feb 2017, 02:00 PM
I was asking about the raw data value, not the header. I have been able to get the custom header working. Where can I find the raw data value, not the formatted one?
0
Stefan
Telerik team
answered on 17 Feb 2017, 10:10 AM
Hi William,

Thank you for this clarification.

In this case, you will need to access the relevant property of the bound item. This can be achieved by using the DataContext of the exported row. Please, take a look at the following code snippet.
private void gridViewExport_ElementExporting(object sender
    , GridViewElementExportingEventArgs e)
{
    if (e.Element == ExportElement.Row)
    {
        var item = ((e.Context as GridViewRow).DataContext as Club);
         
    }
}

I hope this helps.

Regards,
Stefan X1
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
William
Top achievements
Rank 1
answered on 22 Feb 2017, 04:49 PM

I have been able to put in changes to get the name of the custom header. Now, the problem is with the export itself.

When I set SelectedExportFormat="Excel", it creates an "xls" file, but when I open it in Excel, it gives me an error message.

When I set SelectedExportFormat="ExcelML", no error message is created when I open it, but the code to get the custom header name is NOT executed.

Is there a set of code that works completely to give custom headers but also doesn't generate an error when opening in Excel?

 

0
Stefan Nenchev
Telerik team
answered on 27 Feb 2017, 12:13 PM
Hi William,

The issue seems related to the following article from our documentation - Warning message when opening the Excel file. I suggest you go through the Export section in our documentation page as it provides most of the answers you are looking for. Furthermore, the RadGridView exposes a newer method for exporting to Excel - ExportToXlsx. We advise on using it in most cases. You should not get the error with it.

If you are still experiencing some issues, you can provide a sample with the specific setup you are trying to export in a ticket so we can have a more detailed look and advise you appropriately.

Have a great week.

Stefan Nenchev
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
0
William
Top achievements
Rank 1
answered on 10 Mar 2017, 02:56 PM

I installed the code for ExportToXlsx and set the references shown in the documentation, but the method "gridViewExport.ExportToXlsx" does not exist in the context. My version of Telerik is 2016. Is there a full example somewhere

with this new functionality?

0
Stefan Nenchev
Telerik team
answered on 15 Mar 2017, 08:36 AM
Hi William,

The ExportToXlsx method was introduced in 2015 so you should have no problems using it with a later version. Please make sure that you have added all the required assembly references. I have attached a sample for your reference. Please have a look at it and consider such approach from your end as well.


Regards,
Stefan Nenchev
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
0
William
Top achievements
Rank 1
answered on 15 Mar 2017, 01:39 PM
I was able to get this working, but it is NOT an improvement. I can't get the actual value (unformatted), the custom headers show up as "System.Windows.Controls.TextBlock", and I still get an error message when I try to import into Excel. I take it there is NO example that works completely?
0
Stefan Nenchev
Telerik team
answered on 17 Mar 2017, 11:40 AM
Hello William,

As my colleague has already advised, we have exposed different events and modifications to the Export functionality which you can utilize to achieve your behavior. The samples are made to work in the most common scenarios and for more specific setups you need to apply some additional logic. 

The approach for exporting custom headers with the ExportToXlsx method is similar to the one when using the older export method. The only difference is that the ExportToXlsx utilizes the ElementExportingToDocument event instead of the ElementExporting one:

private void clubsGrid_ElementExportingToDocument(object sender, GridViewElementExportingToDocumentEventArgs e)
       {
           if (e.Value != null && e.Value.GetType() == typeof(TextBlock))
               e.Value = (e.Value as TextBlock).Text;
       }
As for the you are receiving, unfortunately, we cannot reproduce such issue from our side. My advice is to raise a ticket with a sample so we can have a more detailed look and advise you.

William, I would like to once again point out that most of this information is available in the online documentation of the Export functionality. Please have a look at it to see the difference between the methods and respectively the events they expose. 

Regards,
Stefan Nenchev
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
William
Top achievements
Rank 1
Answers by
Stefan
Telerik team
William
Top achievements
Rank 1
Stefan Nenchev
Telerik team
Share this question
or