Search results

Print

To print the Grid, use the print method from grid instance. The print option can be displayed on the Toolbar by adding the Print toolbar item.

tagHelper
print.cs
<ejs-grid id="Grid" dataSource=@ViewBag.DataSource toolbar="@(new List<string>() { "Print"})" > 
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City" width="150"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name" width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>
public IActionResult Index()
{
    ViewBag.DataSource = OrderDetails.GetAllRecords();
    return View();
}

Page setup

Some of the print options cannot be configured through JavaScript code. So, you have to customize the layout, paper size, and margin options using the browser page setup dialog. Please refer to the following links to know more about the browser page setup:

To print the grid from an external button, invoke the print method.

tagHelper
external-btn.cs
<ejs-button id="printbtn" content="Print" isPrimary="true"></ejs-button>
<ejs-grid id="Grid" dataSource=@ViewBag.DataSource > 
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City" width="150"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name" width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

<script>

document.getElementById('printbtn').onclick = function () {
    document.getElementById('Grid').ej2_instances[0].print();
}

</script>
public IActionResult Index()
{
    ViewBag.DataSource = OrderDetails.GetAllRecords();
    return View();
}

By default, the grid prints all the pages. To print the current page alone, set the PrintMode to CurrentPage.

tagHelper
current-page.cs
<ejs-grid id="Grid" dataSource=@ViewBag.DataSource toolbar="@(new List<string>() { "Print"})" printMode="CurrentPage" > 
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City" width="150"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name" width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>
public IActionResult Index()
{
    ViewBag.DataSource = OrderDetails.GetAllRecords();
    return View();
}

By default, the grid will be print the master and expanded child grids alone. you can change the print option by using the hierarchyPrintMode property. The available options are,

Mode Behavior
Expanded Prints the master grid with expanded child grids.
All Prints the master grid with all the child grids.
None Prints the master grid alone.
tagHelper
var SecondChildGrid = new Syncfusion.EJ2.Grids.Grid()
    {
        DataSource = (IEnumerable<object>)ViewBag.CustomerDataSource,
        QueryString = "CustomerID",
        Columns = new List<Syncfusion.EJ2.Grids.GridColumn> {
                new Syncfusion.EJ2.Grids.GridColumn(){ Field="CustomerID", HeaderText="Customer ID", Width="90", TextAlign=Syncfusion.EJ2.Grids.TextAlign.Right },
                new Syncfusion.EJ2.Grids.GridColumn(){ Field="ContactName", HeaderText="Name", Width="100" },
                new Syncfusion.EJ2.Grids.GridColumn(){ Field="CompanyName", HeaderText="Company", Width="120" },
                new Syncfusion.EJ2.Grids.GridColumn(){ Field="Country", HeaderText="Country", Width="90" },
        }
    };
    var ChildGrid = new Syncfusion.EJ2.Grids.Grid() {
        DataSource = (IEnumerable<object>)ViewBag.DataSource,
        QueryString = "EmployeeID",
        Columns = new List<Syncfusion.EJ2.Grids.GridColumn> {
                new Syncfusion.EJ2.Grids.GridColumn(){ Field="OrderID", HeaderText="Order ID", Width="120" },
                new Syncfusion.EJ2.Grids.GridColumn(){ Field="Freight", HeaderText="Freight", Width="120", Format="C2", TextAlign=Syncfusion.EJ2.Grids.TextAlign.Right },
                new Syncfusion.EJ2.Grids.GridColumn(){ Field="ShipName", HeaderText="Ship Name", Width="150" },
                new Syncfusion.EJ2.Grids.GridColumn(){ Field="ShipCity", HeaderText="Ship City", Width="120" },
        },
        ChildGrid = SecondChildGrid
    };

<ejs-grid id="HierarchyPrint" dataSource="ViewBag.EmpDataSource" childGrid="ChildGrid" hierarchyPrintMode="Expanded" toolbar="@(new List<string>() {"Print" })" >
    <e-grid-columns>
        <e-grid-column field="EmployeeID" headerText="Employee ID" textAlign="Right" width="125"></e-grid-column>
        <e-grid-column field="FirstName" headerText="Name" width="120"></e-grid-column>
        <e-grid-column field="Title" headerText="Title"  width="170"></e-grid-column>
        <e-grid-column field="City" headerText="City" format='yMd' textAlign="Right" width="135"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

By default, the hierarchy grid prints the expanded child grids from the visible page only. Refer To Print the expanded state grid from all pages

By default, the browser uses A4 as page size option to print pages and to adapt the size of the page the browser print preview will auto-hide the overflowed contents. Hence grid with large number of columns will cut off to adapt the print page.

To show large number of columns when printing, adjust the scale option from print option panel based on your content size.

Scale Option Setting

Show or Hide columns while Printing

You can show a hidden column or hide a visible column while printing the grid using toolbarClick and printComplete events.

In the toolbarClick event, based on args.item.id as grid_print. We can show or hide columns by setting column.visible property to true or false respectively.

In the printComplete event, We have reversed the state back to the previous state.

In the below example, we have CustomerID as a hidden column in the grid. While printing, we have changed CustomerID to visible column and ShipCity as hidden column.

tagHelper
show-hide.cs
<ejs-grid id="Grid" dataSource=@ViewBag.DataSource toolbar="@(new List<string>() { "Print"})" toolbarClick='toolbarClick' printComplete='printComplete' > 
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer ID" visible="false" width="150"></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City" width="150"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name" width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>
<script>
function toolbarClick(args){
    for (var i = 0; i < this.columns.length; i++) {
        if (this.columns[i].field == "CustomerID") {
            this.columns[i].visible = true;
        }
        else if (this.columns[i].field == "ShipCity") {
            this.columns[i].visible = false;
        }
    }
}

function printComplete(args){
    for (var i = 0; i < this.columns.length; i++) {
        if (this.columns[i].field == "CustomerID") {
            this.columns[i].visible = true;
        }
        else if (this.columns[i].field == "ShipCity") {
            this.columns[i].visible = false;
        }
    }
}
</script>
public IActionResult Index()
{
    ViewBag.DataSource = OrderDetails.GetAllRecords();
    return View();
}

Limitations of Printing Large Data

When grid contains large number of data, printing all the data at once is not a best option for the browser performance. Because to render all the DOM elements in one page will produce performance issues in the browser. It leads to browser slow down or browser hang. Grid have option to handle large number of data by Virtualization. However while printing, it is not possible to use virtualization for rows and columns.

If printing of all the data is still needed, we suggest to Export the grid to Excel or CSV or Pdf and then print it from another non-web based application.

See Also