This is a migrated thread and some comments may be shown as answers.

Printing row and the nested table in RadGrid

4 Answers 180 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Raulis
Top achievements
Rank 1
Raulis asked on 12 Jul 2016, 09:05 AM

Good morning,

 

I'm trying to print all the selected rows in a RadGrid together with every nested row. My table contains master table rows and each row, when expanded, contains 4 extra rows. I'm using the code example below for printing but it only prints the selected rows (if the master table row is not expanded and every nested row not selected, it will just print the master table row).

function PrintGrid() {
                var sh = '<%= ClientScript.GetWebResourceUrl(RadGrid1.GetType(),String.Format("Telerik.Web.UI.Skins.{0}.Grid.{0}.css",RadGrid1.Skin)) %>';
                var styleStr = "<html><head><link href = '" + sh + "' rel='stylesheet' type='text/css'></link></head>";
                var MasterTable = $find('<%= RadGrid1.ClientID %>').get_masterTableView();
                var selectedRows = MasterTable.get_selectedItems();
                var htmlcontent = "<table>";
                for (var i = 0; i < selectedRows.length; i++) {
                    var row = selectedRows[i];
                    htmlcontent = htmlcontent + "<tr>" + row.get_element().innerHTML + "</tr>";
                    if (row.get_nestedViews().length > 0) {
                        var nestedSelectedRows = row.get_nestedViews()[0].get_selectedItems();
                        for (var j = 0; j < nestedSelectedRows.length; j++) {
                            var nestedRow = nestedSelectedRows[j];
                            htmlcontent = htmlcontent + "<tr>" + nestedRow.get_element().innerHTML + "</tr>";
                        }
                    }
                }
 
                htmlcontent = styleStr + "<body><div class='RadGrid RadGrid_Metro'>" + htmlcontent + "</table></div></body></html>";
 
                var previewWnd = window.open('about:blank', '', '', false);
                previewWnd.document.open();
                previewWnd.document.write(htmlcontent);
                previewWnd.document.close();
                previewWnd.print();
            }

 

I'm currently lost and don't know how to take this further. Should I expand all the nested rows and select them on print button click, print the document, and then collapse them again or is there a better/faster way to do this?

4 Answers, 1 is accepted

Sort by
0
Raulis
Top achievements
Rank 1
answered on 12 Jul 2016, 11:11 AM

I've also tried a different (server-side) approach to exporting the grid contents to PDF.

Protected Sub rgCharterer_ItemCreated(sender As Object, e As GridItemEventArgs) Handles rg1.ItemCreated
        If rg1.IsExporting Then
            FormatGridItem(e.Item)
        End If
End Sub
 
Protected Sub btnPrint_Click(sender As Object, e As EventArgs)
        Dim headerMiddleCell As String = "HEADER CENTRE STRING"
        Dim headerLeftCell As String = "HEADER LEFT STRING"
 
        rg1.ExportSettings.Pdf.BorderType = GridPdfSettings.GridPdfBorderType.AllBorders
        rg1.ExportSettings.Pdf.PageHeader.MiddleCell.Text = headerMiddleCell
        rg1.ExportSettings.Pdf.PageHeader.MiddleCell.TextAlign = GridPdfPageHeaderFooterCell.CellTextAlign.Center
        rg1.ExportSettings.Pdf.PageHeader.LeftCell.Text = headerLeftCell
        rg1.ExportSettings.Pdf.PageHeader.LeftCell.TextAlign = GridPdfPageHeaderFooterCell.CellTextAlign.Center
 
        rg1.ExportSettings.Pdf.ContentFilter = GridPdfFilter.NoFilter
 
        For Each item As GridDataItem In rg1.MasterTableView.Items
            If Not item.Selected Then
                item.Visible = False
            End If
        Next
 
        rg1.ExportSettings.OpenInNewWindow = True
        rg1.MasterTableView.ExportToPdf()
    End Sub
 
Protected Sub FormatGridItem(item As GridItem)
        item.Style("color") = "#eeeeee"
 
        If TypeOf item Is GridDataItem Then
            item.Style("vertical-align") = "middle"
            item.Style("text-align") = "center"
        End If
 
        Select Case item.ItemType
                'Mimic RadGrid appearance for the exported PDF file
            Case GridItemType.Item
                item.Style("background-color") = "#4F4F4F"
                Exit Select
            Case GridItemType.AlternatingItem
                item.Style("background-color") = "#494949"
                Exit Select
            Case GridItemType.Header
                item.Style("background-color") = "#2B2B2B"
                Exit Select
            Case GridItemType.CommandItem
                item.Style("background-color") = "#000000"
                Exit Select
        End Select
 
        If TypeOf item Is GridCommandItem Then
            'needed to span the image over the CommandItem cells
            item.PrepareItemStyle()
        End If
    End Sub

 

This is KIND OF working, but not really. If I select a row and try to export to PDF, it will export just the row itself with all the images (the row is filled with columns that contain one of 4 images for indicating a status) but not the details table. If I expand the details table and try to print the row then - it only prints the details table without the master row. This is really confusing. Anyone able to help?

0
Konstantin Dikov
Telerik team
answered on 14 Jul 2016, 01:23 PM
Hi Raulis,

I have tested the server-side approach, where all selected items are expanded and everything seems to work correctly with Advanced Data-Binding:
Protected Sub ExportButton_Click(sender As Object, e As EventArgs)
    For Each item As GridDataItem In rg1.MasterTableView.Items
        If Not item.Selected Then
            item.Visible = False
        Else
            item.Expanded = True
        End If
    Next
 
    rg1.ExportSettings.OpenInNewWindow = True
    rg1.MasterTableView.ExportToPdf()
 
End Sub

If you are using Simple Data-Binding, could you please try switching to Advanced Data-Binding and see if it will make any difference.


Regards,
Konstantin Dikov
Telerik by Progress
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Raulis
Top achievements
Rank 1
answered on 18 Jul 2016, 07:46 AM

Hi Konstantin,

I've tried your approach but when the PDF document gets exported the details table prints "No child records to display." Any advice on how to fix this one?

0
Konstantin Dikov
Telerik team
answered on 18 Jul 2016, 11:47 AM
Hi Raulis,

Can you please elaborate how you are binding your RadGrid? You can also post the markup of your RadGrid, so we can have a better idea of your exact scenario. 

Looking forward to your reply.


Regards,
Konstantin Dikov
Telerik by Progress
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Tags
Grid
Asked by
Raulis
Top achievements
Rank 1
Answers by
Raulis
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Share this question
or