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

GridView Export to Excel - Custom Columns

4 Answers 310 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ruta
Top achievements
Rank 1
Ruta asked on 20 Sep 2016, 08:52 PM

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

 

 

4 Answers, 1 is accepted

Sort by
0
Stefan Nenchev
Telerik team
answered on 21 Sep 2016, 09:55 AM
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.
0
Ruta
Top achievements
Rank 1
answered on 21 Sep 2016, 02:43 PM

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;
                }
        }
 

0
Accepted
Stefan Nenchev
Telerik team
answered on 26 Sep 2016, 08:21 AM
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.
0
Ruta
Top achievements
Rank 1
answered on 26 Sep 2016, 06:42 PM

Thanks! 

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

Tags
GridView
Asked by
Ruta
Top achievements
Rank 1
Answers by
Stefan Nenchev
Telerik team
Ruta
Top achievements
Rank 1
Share this question
or