Is there a better way to export only the selected SfGrid rows to Excel?

I've been working with the SfGrid control's export to Excel functionality, and am pretty happy with it. You've made it quite easy to export the filtered results, for both the current page and the entire grid, and for whatever columns I choose to export.

However, if I wish to export just the rows that the user has selected, there is no direct method to do so. Instead, I have to do the following:
  1. Grab the current page number of the grid, for later use.
  2. Grab the list of primary keys of the selected rows from the grid.
  3. Grab the list of selected row indexes from the grid, for later use.
  4. Filter the grid on the primary key column, providing the list of keys with an "equal" operator and an "or" predicate. This causes the display of the grid to update to show just the selected rows, after making a call to the controller to get just those rows. It also loses the user's page position in the grid.
  5. I can then export the rows, using an export type of AllPages.
  6. I then have to:
    a) clear the filtering on the primary key column (which causes another call out to the controller),
    b) navigate back to the page of the grid recorded in step 1
    c) reselect the rows based on the list of selected row indexes captured in step 2, so that the user is looking at where they were in the grid before the export
While this works, there's a lot of stuff happening before and after the export that seems unnecessary to me, given that the rows I want to export are already available in the grid's retrieved data.

Is it possible to add functionality and a new ExportType of "SelectedRecords"?

5 Replies

RN Rahul Narayanasamy Syncfusion Team June 9, 2021 01:01 PM UTC

Hi Ted, 

Greetings from Syncfusion. 
 
Query: Is there a better way to export only the selected SfGrid rows to Excel?  

We have validated your query and you want to export the selected records to Excel. You can achieve your requirement by using DataSource property of the ExcelExportProperties class. We have get the selected records by using GetSelectedRecords method and provide the selected data to the DataSource property of ExcelExportProperties. Find the below code snippets and sample for your reference. 

 
<SfGrid ID="Grid" @ref="DefaultGrid" DataSource="@Orders" AllowExcelExport="true" AllowPaging="true" 
        Toolbar="@(new List<string>() { "ExcelExport" })" AllowSelection="true"> 
    <GridEvents OnToolbarClick="OnToolbarClick" TValue="Order"></GridEvents> 
    <GridSelectionSettings Type="SelectionType.Multiple"></GridSelectionSettings> 
    <GridColumns> 
        . . . 
    </GridColumns> 
</SfGrid> 
 
@code{ 
    private SfGrid<Order> DefaultGrid; 
 
    public List<Order> Orders { get; set; } 
 
    public async Task OnToolbarClick(Syncfusion.Blazor.Navigations.ClickEventArgs args) 
    { 
        if (args.Item.Id == "Grid_excelexport") //Id is combination of Grid's ID and itemname 
        { 
            ExcelExportProperties ExcelProperties = new ExcelExportProperties(); 
            var selected = await DefaultGrid.GetSelectedRecords(); 
            if(selected.Count() > 0) 
            { 
                ExcelProperties.DataSource = selected;    //provided selected data to Excelproperties DataSource 
            } 
            else 
            { 
                ExcelProperties.DataSource = Orders;   //if there is no selected records, then provided Whole Grid datasource 
            } 
            await this.DefaultGrid.ExcelExport(ExcelProperties); 
        } 
    } 
    . . . 
} 


Reference

Please let us know if you have any concerns. 

Regards, 
Rahul 



TS Ted Statham June 9, 2021 02:17 PM UTC

I tried your suggested method prior to initiating this thread, as it is in your online documentation for the Excel export functionality. I'm sure it works if the grid's DataSource is set to an in-memory enumerable collection.

However, in my situation, I'm using a grid which sources its data through the SfDataManager, speaking through an ODataV4 adaptor. In this configuration, it ignores whatever is assigned to the ExcelExportProperties.DataSource property, and instead calls out to the ODataV4 adaptor. The resulting Excel document includes whatever filtered data the grid is set to retrieve through the adaptor. I suspect it doesn't matter what adaptor is in use, but I haven't ventured to test such a case.


RN Rahul Narayanasamy Syncfusion Team June 10, 2021 07:33 AM UTC

Hi Ted, 
 
Thanks for the update. 
 
We have validated your query and confirmed it as a bug and logged the defect report Problem with custom datasource exporting when bind data using SfDataManager to Grid” for the same. Thank you for taking the time to report this issue and helping us improve our product. At Syncfusion, we are committed to fixing all validated defects (subject to technological feasibility and Product Development Life Cycle) and including the defect fix in our weekly release which is expected to be rolled out by the mid of July, 2021.   
        
You can now track the current status of your request, review the proposed resolution timeline, and contact us for any further inquiries through this link. 
 
 
Please let us know if you have any concerns. 
 
Regards, 
Rahul 
  



TS Ted Statham June 10, 2021 01:48 PM UTC

Thanks for kicking this up to a bug. I look forward to seeing it resolved, as it will simplify my code greatly.


RN Rahul Narayanasamy Syncfusion Team June 11, 2021 05:20 AM UTC

Hi Ted, 
 
Thanks for the update. 
 
We will let you know once the fix in included in the release. Until then we appreciate your patience. 
 
Regards, 
Rahul 


Loader.
Up arrow icon