SfDataGrid View is null when attempting to export to Excel

Hello,  I am running into a problem when using the Export to Excel functionality for SfDataGrid.  Normally the export functions properly as follows:

datagrid.ExportToExcel(datagrid.View, options)

However in my case the data grid is in a TabItemExt control, on the second (hidden) tab.  So datagrid.View is null, unless I manually click the tab to bring
it into view.

The data grid is also unique - it uses code-behind to create the result I need.  Here is an example of what I mean. 

<syncfusion:SfDataGrid x:Name="dgEmployees" >
            <syncfusion:SfDataGrid.Columns>
                        <syncfusion:GridTextColumn HeaderText="Vendor" MappingName="VendorName" />
            </syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>

and in the code-behind I create and bind additional columns dynamically:

 foreach (string countryName in countryCollection)
            {
                Syncfusion.UI.Xaml.Grid.GridImageColumn x = new Syncfusion.UI.Xaml.Grid.GridImageColumn();

                x.HeaderText = countryName;
                x.ValueBinding = new Binding("Employees[" + countryName + "]");

                dgEmployees.Columns.Add(x);

            }

dgEmployees.ItemsSource = vm.CountryMappings;


How can I set the dgEmployees.View even if it is not visible? 

thanks,




5 Replies

SP Shobika Palani Syncfusion Team November 2, 2018 06:24 PM UTC

Hi Milan, 

Thank you for contacting Syncfusion Support. 

We have analyzed your query to export datagrid that is not visible. You can achieve your requirement by calling ApplyTemplate method before exporting as like below code snippet 

ExcelExportingOptions options = new ExcelExportingOptions();  
options.ExcelVersion = ExcelVersion.Excel2010;  
dataGrid.ApplyTemplate();  
var excelEngine = dataGrid.ExportToExcel(options);  
var workBook1 = excelEngine.Excel.Workbooks[0];  
var worksheet1 = workBook1.Worksheets[0];  
workBook1.SaveAs("sample.xlsx"); 

Please find sample from below link and let us know if this helps you 
Sample Link : 

Regards, 
Shobika. 



AL Aldin October 29, 2021 07:22 AM UTC

Hi


I am having the same issue in Xamarin Forms, but AppyTemplate is not found in sfDataGrid


Any alternative?


Thanks


Aldin Cedeño



SV Suja Venkatesan Syncfusion Team November 1, 2021 03:10 PM UTC

Hi Aldin, 

Thanks for the update. 

You achieve your requirement by calling createCollectionView method before calling ExportToExcel method as like below code snippet. 

Code Snippet: 
private void ExportToExcel_Clicked(object sender, EventArgs e) 
{ 
DataGridExcelExportingController excelExport = new DataGridExcelExportingController(); 
if (!sfGrid.IsVisible) 
{ 
  sfGrid.GetType().GetMethod("CreateCollectionView", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Invoke(sfGrid, new object[] { sfGrid.ItemsSource }); 
 } 
var excelEngine = excelExport.ExportToExcel(this.sfGrid); 
var workbook = excelEngine.Excel.Workbooks[0]; 
MemoryStream stream = new MemoryStream(); 
… 


We have prepared a sample to achieve your requirement and attached it in the following link for your reference . 


Please have look at this sample and let us know if you have any concern in this. 

Regards, 
Suja 



AL Aldin replied to Suja Venkatesan November 2, 2021 10:38 PM UTC

Hi Suja


It has worked perfectly


Thank you so much


Regards,

Aldin



VS Vijayarasan Sivanandham Syncfusion Team November 3, 2021 05:58 AM UTC

Hi Aldin,

Thanks for the update.

We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you😊.

Regards,
Vijayarasan S


Loader.
Up arrow icon