GridView Export to Excel - Custom Columns

5 posts, 1 answers
  1. Ruta
    Ruta avatar
    3 posts
    Member since:
    May 2016

    Posted 20 Sep Link to this post

    We have custom columns in our gridviews as one below. When exporting to Excel, the key values are exported instead of displays values.

    <lookups:GridViewProjectLookupColumn Header="Project Number"
                                                           DataMemberBinding="{Binding ProjectRecId}"
                                                           DisplayMemberBinding="{Binding Project_ProjectRecId}"
                                                           DisplayMemberPath="ProjId"
                                                           SortMemberPath="Project_ProjectRecId.ProjId"
                                                           FilterMemberPath="Project_ProjectRecId.ProjId"
                                                           ProjectTypeVisible="False"
                                                           CustomerAccountVisible="False"
                                                           ProjectStageVisible="False"
                                                           IsReadOnlyBinding="{Binding IsManualReadOnly}" />

    Is there a way to export the value of DisplayMemeberPath (ProjId) instead of the DataMemeberBinding( ProjectRecId)?

     

    Thanks,

    Rūta

     

     

  2. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    277 posts

    Posted 21 Sep Link to this post

    Hi Ruta,

    As far as I understand you are using the ExportToXlsx method of RadGridView, is this correct? If this is the case, you can utilize the ElementExportingToDocument event and change the value of the cell. For example:

    private void ClubsGrid_ElementExportingToDocument(object sender, GridViewElementExportingToDocumentEventArgs e)
          {
              if (e.Element == ExportElement.Cell)
              {
                  var cellExportingArgs = e as GridViewCellExportingEventArgs;
                  if (cellExportingArgs.Column == this.clubsGrid.Columns[0])
                  {
                //Change the value to another property of the business object
                      e.Value = (e.DataContext as Club).StadiumCapacity;
                  }
              }
          }

    I have added a sample project for your convenience.

    Regards,
    Stefan Nenchev
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Ruta
    Ruta avatar
    3 posts
    Member since:
    May 2016

    Posted 21 Sep in reply to Stefan Nenchev Link to this post

    Thanks, Stefan

     

    You are assuming correctly - we are using ExportToXlsx and utilizing ElementExportingToDocumenti event. The problem that I run into is that I would have to know which column is getting exported to use the corresponding related business object property.

    I was wondering, if there is a way to get another property from the business object without writing a case statement to check the column being exported. Similar to your modified example below?

    private void ClubsGrid_ElementExportingToDocument(object sender, GridViewElementExportingToDocumentEventArgs e)
            {
                if (e.Element == ExportElement.Cell)
                {
                    var cellExportingArgs = e as GridViewCellExportingEventArgs;
                    var column = cellExportingArgs.Column as GridViewLookupColumnBase; //this our the custom column type
                    if (column != null)
                    {
                        var relatedBusinessObjectProperty = column.DisplayMemeberPath; //can I get the property from DisplayMemeberPath or similar?
                        e.Value == (e.DataContext as Club).relatedBusinessObjectProperty;
                    }
            }
     

  5. Answer
    Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    277 posts

    Posted 26 Sep Link to this post

    Hi Ruta,

    You can use reflection in order to get the value of the specific property you intend to export:

    private void ClubsGrid_ElementExportingToDocument(object sender, GridViewElementExportingToDocumentEventArgs e)
          {
              if (e.Element == ExportElement.Cell)
              {
                  var cellExportingArgs = e as GridViewCellExportingEventArgs;
                  if (cellExportingArgs.Column is ExportColumn)
                  {
                      var exportProperty = (cellExportingArgs.Column as ExportColumn).ExportPath;
                      e.Value =  GetExportedValue(e.DataContext, exportProperty);
                  }
              }
          }
     
          private object GetExportedValue(object src, string exportProperty)
          {
               return src.GetType().GetProperty(exportProperty).GetValue(src, null);
          }

    I have updated the sample project for your convenience.

    Regards,
    Stefan Nenchev
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  6. Ruta
    Ruta avatar
    3 posts
    Member since:
    May 2016

    Posted 26 Sep in reply to Stefan Nenchev Link to this post

    Thanks! 

    That worked. I had to modify it to get the related entity property (property of the property), but it worked!

Back to Top
UI for WPF is Visual Studio 2017 Ready