Exporting the Selected Records

17 Feb 20222 minutes to read

You can export the selected records data by passing it to dataSource of PdfExportProperties in the ToolbarClick event.

In the below exporting demo, We can get the selected records using getSelectedRecords method and pass the selected data to PdfExport or excelExport property.

@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.DataSource).AllowPdfExport().AllowExcelExport().ToolbarClick("toolbarClick").Columns(col =>
{
   col.Field("OrderID").HeaderText("Order ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
   col.Field("OrderDate").HeaderText("Order Date").Width("130").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Format("yMd").Add();
   col.Field("Freight").HeaderText("Freight").Width("120").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("ShipCountry").HeaderText("Ship Country").Width("120").Add();

}).AllowPaging().PageSettings(page => page.PageSize(5).PageCount(5)).AllowFiltering().Toolbar(new List<string>() { "PdfExport", "ExcelExport" }).Render()

<script>
    function toolbarClick(args) {
        var gridObj = document.getElementById("Grid").ej2_instances[0];
        if (args.item.id === 'Grid_pdfexport') {
            var pdfdata = gridObj.getSelectedRecords();
            var exportProperties = {
                dataSource: pdfdata
            };
            gridObj.pdfExport(exportProperties);
        }
        if (args.item.id === 'Grid_excelexport') {
            var exceldata = gridObj.getSelectedRecords();
            var exportProperties = {
                dataSource: exceldata
            };
            gridObj.excelExport(exportProperties);
        }
    }
</script>
public IActionResult Index()
{
    ViewBag.DataSource = OrderDetails.GetAllRecords();
    return View();
}