Export example not working.

18 posts, 0 answers
  1. William
    William avatar
    35 posts
    Member since:
    Sep 2016

    Posted 27 Jan Link to this post

    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.

  2. Stefan X1
    Admin
    Stefan X1 avatar
    606 posts

    Posted 01 Feb Link to this post

    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.
  3. William
    William avatar
    35 posts
    Member since:
    Sep 2016

    Posted 01 Feb in reply to Stefan X1 Link to this post

    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.

     

  4. Stefan X1
    Admin
    Stefan X1 avatar
    606 posts

    Posted 06 Feb Link to this post

    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.
  5. William
    William avatar
    35 posts
    Member since:
    Sep 2016

    Posted 06 Feb in reply to Stefan X1 Link to this post

    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:

     

  6. William
    William avatar
    35 posts
    Member since:
    Sep 2016

    Posted 06 Feb in reply to Stefan X1 Link to this post

    The second type of export (SelectedExportFormat="ExcelML") doesn't have the correct headers, either:
  7. Stefan X1
    Admin
    Stefan X1 avatar
    606 posts

    Posted 09 Feb Link to this post

    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.
  8. William
    William avatar
    35 posts
    Member since:
    Sep 2016

    Posted 09 Feb in reply to Stefan X1 Link to this post

    Yes, there are custom headers. I am using your example "GridView.Exporting". Where do I put the code for the header?
  9. William
    William avatar
    35 posts
    Member since:
    Sep 2016

    Posted 09 Feb in reply to Stefan X1 Link to this post

    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)?
  10. Stefan X1
    Admin
    Stefan X1 avatar
    606 posts

    Posted 14 Feb Link to this post

    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.
  11. William
    William avatar
    35 posts
    Member since:
    Sep 2016

    Posted 14 Feb in reply to Stefan X1 Link to this post

    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?
  12. Stefan X1
    Admin
    Stefan X1 avatar
    606 posts

    Posted 17 Feb Link to this post

    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.
  13. William
    William avatar
    35 posts
    Member since:
    Sep 2016

    Posted 22 Feb in reply to Stefan X1 Link to this post

    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?

     

  14. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    355 posts

    Posted 27 Feb Link to this post

    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 allow you to write beautiful native mobile apps using a single shared C# codebase.
  15. William
    William avatar
    35 posts
    Member since:
    Sep 2016

    Posted 10 Mar in reply to Stefan Nenchev Link to this post

    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?

  16. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    355 posts

    Posted 15 Mar Link to this post

    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 allow you to write beautiful native mobile apps using a single shared C# codebase.
  17. William
    William avatar
    35 posts
    Member since:
    Sep 2016

    Posted 15 Mar in reply to Stefan Nenchev Link to this post

    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?
  18. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    355 posts

    Posted 17 Mar Link to this post

    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 error 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 allow you to write beautiful native mobile apps using a single shared C# codebase.
Back to Top