We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

dataGrid.View = null when SfGrid inizialized manually, unable to Export to PDF and Xlsx

Dear Syncfusion, community, 
I'm a beginner in UWP world and I'm developing a simple app to evaluate the average mark of some exams. 

The object is defined as :

  public class mark
        {
            public float Voto { get; set; }

            public mark(float voto)
            {
                Voto = voto;
            }

        }

        public List<mark> GetMarks()
        {
            List<mark> marks = new List<mark>();

            foreach (float item in App.Model.Exams.Select(p => p.Mark).ToArray())
            {
                marks.Add(new mark(item));
            }

            return marks;

        }


I would like the app to be able to save the list of exam in .xlsx and .pdf format. I tried with two solutions.

1)

I can use successfully the using Syncfusion.XlsIO library to save a .xlsx file as:
 async private void ShareNotes(object sender, RoutedEventArgs e)
        {
            //Share note will provide the possibility to share a xls or PDF file with all the 
            // career and data

            ExcelEngine excelEngine = new ExcelEngine();
            IApplication application = excelEngine.Excel;
            application.DefaultVersion = ExcelVersion.Excel2013;
            IWorkbook workbook = application.Workbooks.Create(1);
            IWorksheet worksheet = workbook.Worksheets[0];


            worksheet.Range["A1"].Text = "Hello World";

            IList<mark> marks = GetMarks();

            //Purtroppo il metodo vuole un costruttore diverso dell'oggetto
            worksheet.ImportData(marks, 1, 2, false);
            worksheet.ImportData(marks, 3, 5, false);


            StorageFile storageFile = await KnownFolders.CameraRoll.CreateFileAsync("Sample1" + ".xlsx", CreationCollisionOption.ReplaceExisting);

            if (storageFile != null)
                await workbook.SaveAsAsync(storageFile);
        }

Problem
I would like convert the excel workbook into a PDF and export. Unfortunately reading the feature matrix of the PDF section in 
https://www.syncfusion.com/products/file-formats/pdf
seems that this feature is not yet implemented for UWP

2) The other solution is therefore to create SfDataGrid, without displaying the Grid it in my app (since I'm displaying data with other solution), and use the ExportToPDF and ExportToExcel function. They are implemented also for UWP and I should be able to convert the datagrid into a PDF and save it.

If, just to try, I display the chart defining it in xaml everything works:
xaml
  <syncfusion:SfDataGrid x:Name="dataGrid"
                               AutoGenerateColumns="True"
                               ItemsSource="{Binding}" /> 
Code Behind
 IList<mark> marks = GetMarks();

            
            dataGrid.ItemsSource = marks;
          
            var document = dataGrid.ExportToPdf();

            StorageFile storageFile = await KnownFolders.CameraRoll.CreateFileAsync("Sample1" + ".pdf", CreationCollisionOption.ReplaceExisting);

            if (storageFile != null)
                await document.SaveAsync(storageFile);





 

4 Replies

FR Francesco May 8, 2016 03:31 PM UTC

Sorry I haven't finished to write all the question before.
Here below the complete one:

Dear Syncfusion, community, 
I'm a beginner in UWP world and I'm developing a simple app to evaluate the average mark of some exams. 

The object is defined as :

  public class mark
        {
            public float Voto { get; set; }

            public mark(float voto)
            {
                Voto = voto;
            }

        }

        public List<mark> GetMarks()
        {
            List<mark> marks = new List<mark>();

            foreach (float item in App.Model.Exams.Select(p => p.Mark).ToArray())
            {
                marks.Add(new mark(item));
            }

            return marks;

        }


I would like the app to be able to save the list of exam in .xlsx and .pdf format. I tried with two solutions.

1)

I can use successfully the using Syncfusion.XlsIO library to save a .xlsx file as:
 async private void ShareNotes(object sender, RoutedEventArgs e)
        {
            //Share note will provide the possibility to share a xls or PDF file with all the 
            // career and data

            ExcelEngine excelEngine = new ExcelEngine();
            IApplication application = excelEngine.Excel;
            application.DefaultVersion = ExcelVersion.Excel2013;
            IWorkbook workbook = application.Workbooks.Create(1);
            IWorksheet worksheet = workbook.Worksheets[0];


            worksheet.Range["A1"].Text = "Hello World";

            IList<mark> marks = GetMarks();

            //Purtroppo il metodo vuole un costruttore diverso dell'oggetto
            worksheet.ImportData(marks, 1, 2, false);
            worksheet.ImportData(marks, 3, 5, false);


            StorageFile storageFile = await KnownFolders.CameraRoll.CreateFileAsync("Sample1" + ".xlsx", CreationCollisionOption.ReplaceExisting);

            if (storageFile != null)
                await workbook.SaveAsAsync(storageFile);
        }

Problem
I would like convert the excel workbook into a PDF and export. Unfortunately reading the feature matrix of the PDF section in 
https://www.syncfusion.com/products/file-formats/pdf
seems that this feature is not yet implemented for UWP

2) The other solution is therefore to create SfDataGrid, without displaying the Grid it in my app (since I'm displaying data with other solution), and use the ExportToPDF and ExportToExcel function. They are implemented also for UWP and I should be able to convert the datagrid into a PDF and save it.

If, just to try, I display the chart defining it in xaml everything works:
xaml

  <syncfusion:SfDataGrid x:Name="dataGrid"
                               AutoGenerateColumns="True"
                               ItemsSource="{Binding}" /> 
Code Behind

        IList<mark> marks = GetMarks();

            
            dataGrid.ItemsSource = marks;
          
            var document = dataGrid.ExportToPdf();

            StorageFile storageFile = await KnownFolders.CameraRoll.CreateFileAsync("Sample1" + ".pdf", CreationCollisionOption.ReplaceExisting);

            if (storageFile != null)
                await document.SaveAsync(storageFile);


Problem
If I define manually the sfchart ( Remeber that I don't want to display it)

            IList<mark> marks = GetMarks();

            SfDataGrid dataGrid = new SfDataGrid();
            dataGrid.ItemsSource = marks;
            dataGrid.AutoGenerateColumns = true;
            



            var document = dataGrid.ExportToPdf();

            StorageFile storageFile = await KnownFolders.CameraRoll.CreateFileAsync("Sample1" + ".pdf", CreationCollisionOption.ReplaceExisting);

            if (storageFile != null)
                await document.SaveAsync(storageFile);


The PDF tourns out to be empty.

The same thing if I try to save a .xlsx as

  IList<mark> marks = GetMarks();

            SfDataGrid dataGrid = new SfDataGrid();
            dataGrid.ItemsSource = marks;
            dataGrid.AutoGenerateColumns = true;
            

            var options = new ExcelExportingOptions();

            options.ExcelVersion = ExcelVersion.Excel2013;

            var excelEngine = dataGrid.ExportToExcel(dataGrid.View, options);

            var workBook = excelEngine.Excel.Workbooks[0];
            

            StorageFile storageFile = await KnownFolders.CameraRoll.CreateFileAsync("Sample" + ".xlsx", CreationCollisionOption.ReplaceExisting);

            if (storageFile != null)
                await workBook.SaveAsAsync(storageFile);
 
In this case in particular dataGrid.View is null



I'm including 
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Syncfusion.UI.Xaml.Grid;
using Syncfusion.UI.Xaml.Grid.Converter;
using Syncfusion.XlsIO;
using Syncfusion.Pdf;
using Syncfusion.Data.Extensions;


using Windows.Storage;
using System.Collections.ObjectModel;



Thanks for your help.
Francesco


JG Jai Ganesh S Syncfusion Team May 10, 2016 04:17 AM UTC

Hi Francesco, 
 
Please find the responses for your queries as below, 
 
Query 1: 
We regret to let you know that currently we do not have support for Excel to PDF conversion in UWP platform. However, we have already added it to our Feature request list and it will be implemented in one of the upcoming releases. 
Query 2: 
You can achieve your requirement for Exporting a SfDataGrid to Excel at runtime by using the below code, 
private async void Button_Click(object sender, RoutedEventArgs e) 
        { 
 
            SfDataGrid grid = new SfDataGrid(); 
            
            GridQueryableCollectionViewWrapper wrapper = new GridQueryableCollectionViewWrapper((this.DataContext as OrderInfoViewModel).OrdersListDetails, grid); 
            grid.ItemsSource = wrapper; 
 
            grid.Columns.Add(new GridTextColumn() { MappingName = "CustomerID" }); 
            grid.Columns.Add(new GridTextColumn() { MappingName = "Quantity" }); 
             
            grid.ApplyTemplate(); 
 
            var options = new ExcelExportingOptions(); 
 
            options.ExcelVersion = ExcelVersion.Excel2013; 
 
            var excelEngine = grid.ExportToExcel(wrapper, options); 
 
 
            var workBook = excelEngine.Excel.Workbooks[0]; 
 
            var savePicker = new FileSavePicker 
            { 
                SuggestedStartLocation = PickerLocationId.Desktop, 
                SuggestedFileName = "Sample" 
            }; 
 
            if (workBook.Version == ExcelVersion.Excel97to2003) 
            { 
                savePicker.FileTypeChoices.Add("Excel File (.xls)", new List<string>() { ".xls" }); 
            } 
            else 
            { 
                savePicker.FileTypeChoices.Add("Excel File (.xlsx)", new List<string>() { ".xlsx" }); 
            } 
 
            var storageFile = await savePicker.PickSaveFileAsync(); 
 
            if (storageFile != null) 
            { 
                await workBook.SaveAsAsync(storageFile); 
 
                var msgDialog = new MessageDialog("Do you want to view the Document?", "File has been created successfully."); 
 
                var yesCmd = new UICommand("Yes"); 
                var noCmd = new UICommand("No"); 
                msgDialog.Commands.Add(yesCmd); 
                msgDialog.Commands.Add(noCmd); 
                var cmd = await msgDialog.ShowAsync(); 
                if (cmd == yesCmd) 
                { 
                    // Launch the saved file 
                    bool success = await Windows.System.Launcher.LaunchFileAsync(storageFile); 
                } 
            } 
            excelEngine.Dispose(); 
        } 
In the above sample, we have created the columns manually and call the ApplyTemplate(). 
Regards, 
Jai Ganesh S 



FR Francesco May 10, 2016 09:58 PM UTC

Thank you very much for the fast reply. It has work properly for the Excel file, but i still have problems in converting to PDF the grid.

   var optionsPDF = new PdfExportingOptions();

            optionsPDF.AutoColumnWidth = true;

            var document = grid.ExportToPdf(wrapper, optionsPDF);

            StorageFile storageFilePDF = await KnownFolders.CameraRoll.CreateFileAsync("Sample" + ".pdf", CreationCollisionOption.ReplaceExisting);

            if (storageFilePDF != null)
                await document.SaveAsync(storageFilePDF);

Null exception is risen.



JG Jai Ganesh S Syncfusion Team May 12, 2016 02:03 AM UTC

Hi Francesco, 
 
A support incident to track the status of this defect has been created under your account. Please log on to our support website to check for further updates. 
 
 
Regards, 
Jai Ganesh S 


Loader.
Up arrow icon