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
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
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.
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
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:
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
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
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
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?
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
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?
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
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;
}
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