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
close icon

Print Selected Rows in MVC

I want to print (in printer) the selected rows of a Grid, I uploaded a image file with the example. But when I click on the printer icon, it prints the entire grid. 

This is my Grid
 @(Html.EJ().Grid<BitacoraR>("GridBitacorabyIngeniero")
                .Datasource((IEnumerable<BitacoraR>)ViewBag.BitacorabyIngeniero)
                .Locale("es-MX")
                 .AllowPaging()
                 .AllowScrolling()
                  .CssClass("CustomCss")
                .PageSettings(p => { p.PageSize(16); })
                .ScrollSettings(col => { col.Width(1150); })
                .SelectionType(SelectionType.Multiple)
                .AllowSelection()
                .IsResponsive()
                .EnableResponsiveRow()
                .AllowFiltering()
                .FilterSettings(filter => { filter.FilterType(FilterType.Excel); })
                .ToolbarSettings(toolbar =>
                {
                    toolbar.ShowToolbar().ToolbarItems(items =>
                    {
                        items.AddTool(ToolBarItems.ExcelExport);
                        items.AddTool(ToolBarItems.PrintGrid);
                    });
                })      
            .Columns(col =>
            {
                col.Field("Id").IsPrimaryKey(true).Visible(false).Add();                               
                col.Field("Date").HeaderText("Fecha").Format("{0:dd/MM/yyyy}").Width(110).TextAlign(TextAlign.Center).HeaderTextAlign(TextAlign.Left).Add();
                col.Field("Nombre_Completo").HeaderText("Cliente").Width(147).TextAlign(TextAlign.Center).HeaderTextAlign(TextAlign.Left).Add();
                col.Field("Nombre_Cliente").HeaderText("Cliente").Width(147).TextAlign(TextAlign.Center).HeaderTextAlign(TextAlign.Left).Add();
                col.Field("Nombre_Locacion").HeaderText("Locación").Width(180).TextAlign(TextAlign.Left).HeaderTextAlign(TextAlign.Left).Add();
                col.Field("Estatus").HeaderText("Estatus").Width(180).TextAlign(TextAlign.Center).HeaderTextAlign(TextAlign.Left).Add();
            })
            .ClientSideEvents(evt => evt.ToolbarClick("OnToolbarClick"))
    )

I tried to use the export solucion for this situation, but it doesn't work, try to use the example provided in this page https://www.syncfusion.com/kb/6332/how-to-export-current-page-selected-record, but it doen't work.

This is my function for the ToolbarClick event, the export part works just fine. It exports the selected rows
function OnToolbarClick(args) {
            if (args.itemName.indexOf("Print") > -1) {//if no selectedRecords, currenviewdata will be exported
                var currentData = JSON.stringify(this.model.selectedRecords.length == 0 ? this.model.currentViewData : this.model.selectedRecords);
                args.cancel = true;
                ej.print(currentData);
            }
            if (args.itemName.indexOf("Export") > -1) {//if no selectedRecords, currenviewdata will be exported
                this.model["currentData"] = JSON.stringify(this.model.selectedRecords.length == 0 ? this.model.currentViewData : this.model.selectedRecords);
            }
        }

Could you please help me?

Attachment: grid_902d4989.rar

5 Replies

SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team March 15, 2017 09:19 AM UTC

Hi Dayne, 
 
Thanks for using Syncfusion Product. We have achieved requirement “Printing Selected Rows from the Grid”. in the BeforePrint event of the Grid, Grid rows were removed and appended only the selected rows in the Grid. Refer to the following code example and Help Document. 
 
@(Html.EJ().Grid<OrdersView>("Grid") 
        .AllowPaging() 
        .ToolbarSettings(tools => 
        { 
            tools.ShowToolbar().ToolbarItems(items => 
            { 
                items.AddTool(ToolBarItems.PrintGrid); 
            }); 
        }) 
        .ClientSideEvents(events => events.BeforePrint("onBeforePrint")) 
) 
 
<script> 
    function onBeforePrint(args){ 
        var gridInst = $("#Grid").ejGrid("instance"); 
        var selRows = gridInst.getContentTable().find('tr[aria-selected="true"]'); 
        args.element.find(".e-gridcontent tbody tr").remove(); 
        args.element.find(".e-gridcontent tbody").append(selRows); 
        gridInst.refreshContent(); 
    } 
</script> 
 
 
We have prepared a demo sample that can be referred from the following location. 
 
 
Regards,  
Seeni Sakthi Kumar S. 



DA Dayne March 15, 2017 03:27 PM UTC

Hi, the solution works with the specifications I provided, but if the Grid has more than one page, and I select 3 rows from the first page and 5 from the second, when I click print it only prints the selected rows of the current page, and I want to print all the selected rows in all the pages, and if I haven't  selected any, print the model dataSource.

Thanks in advance


SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team March 16, 2017 12:09 PM UTC

Hi Dayne, 
 
We achieved your requirement “Printing selected rows from the multiple pages of the Grid” using the ActionBegin event of the Grid. In the ActionBegin event, we have collected the selected rows while navigating to the other pages. So the selected records will be saved to window variable and later in the BeforePrint event of the Grid, it has been used for printing the Grid. We have also ensured whole Grid will be printed if no records were selected in the Grid. Refer to the following code example. 
 
@(Html.EJ().Grid<OrdersView>("Grid")  
        .AllowPaging()  
        .ToolbarSettings(tools =>  
        {  
            tools.ShowToolbar().ToolbarItems(items =>  
            {  
                items.AddTool(ToolBarItems.PrintGrid);  
            });  
        })  
        .ClientSideEvents(events => events.BeforePrint("onBeforePrint").ActionBegin("actionBegin"))  
) 
 
    <script type="text/javascript">  
        window.selectedRows = []; 
        function printSelectedRows(selRows) { 
            if (!selectedRows.length) selectedRows = selRows; 
            else { 
                for (var sr = 0; sr < selRows.length; sr++) { 
                    selectedRows.push(selRows[sr]); 
                } 
            } 
        } 
        function actionBegin(args) { 
            if (args.requestType == "paging") { 
                if (this.getSelectedRows().length) { 
                    var selRows = this.getSelectedRows(); 
                    printSelectedRows(selRows); 
                } 
            } 
        } 
        function beforePrint(args) { 
            var gridInst = $("#Grid").ejGrid("instance"); 
            var selRows = gridInst.getContentTable().find('tr[aria-selected="true"]'); 
            if (selectedRows.length || selRows.length) { 
                if (selRows.length) printSelectedRows(selRows); 
                args.element.find(".e-gridcontent tbody tr").remove(); 
                args.element.find(".e-gridcontent tbody").append(selectedRows); 
                gridInst.refreshContent(); 
            } 
        } 
    </script> 
 
 
We have modified the sample demo that can be referred from the following location. 
 
 
Regards,  
Seeni Sakthi Kumar S. 



DA Dayne March 17, 2017 04:31 PM UTC

This solution works. Thanks for the answer.


FS Farveen Sulthana Thameeztheen Basha Syncfusion Team March 20, 2017 02:21 PM UTC

Hi Dayne, 
  
We are glad to hear that our solution works. Please get back to us if you need any further assistance. 
  
Regards, 
  
Farveensulthana T 


Loader.
Live Chat Icon For mobile
Up arrow icon