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

print radgridview with report (winforms)

11 Answers 335 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dra Graca Melo
Top achievements
Rank 1
Dra Graca Melo asked on 15 Apr 2010, 03:19 PM
How can I put a button to print the content of a radgridview with a telerik report ?

11 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 16 Apr 2010, 05:31 PM
Hi Dra Graca Melo,

Please take a look at this code library article. It demonstrates how to generate reports for RadGridView using Telerik Reporting. I hope this helps.

Best wishes,
Jack
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Dra Graca Melo
Top achievements
Rank 1
answered on 16 Apr 2010, 05:40 PM
Thank you. But I can't add the radgridreportinglite.dll as a project reference.
How can I add the radgridreportinglite.dll ?
0
Martin Vasilev
Telerik team
answered on 21 Apr 2010, 02:31 PM
Hi Dra Graca Melo,

Thank you for getting back to us.

There is no reason which to prevent adding the RadGridVReportingLite.dll as a reference in your project. Just use the browse option in the Visual Studio's Add Reference Dialog Box.
 

Kind regards,
Martin Vasilev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Dra Graca Melo
Top achievements
Rank 1
answered on 23 Apr 2010, 04:22 PM
Thank you, but I donĀ“t  know where to find the file RadGridReportingLite.dll

0
Martin Vasilev
Telerik team
answered on 26 Apr 2010, 02:41 PM
Hi Dra Graca Melo,

Thank you for getting back to me.

Recently, we have added the latest revision of RadGridReportingLite library, which works with Q1 2010 SP1 releases. You can find the project files along with RadGridReportingLite.dll in our Code Library section.

Let me know if you have any additional questions.

Kind regards,
Martin Vasilev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Doug
Top achievements
Rank 2
answered on 25 Jul 2011, 09:33 PM
I have the Q2 2011 WinForm Controls and Telerik Reporting.  I have a radGridView which contains multiple records where each has child records.  I have created a process where I can checkbox the child records I want and then refresh the grid with the selected Parent, Child records.  I now wish to print these records using a Telerik Report.  The RadGridViewReporting.dll article states that it is now obsolete and that if I have the tools mentioned above I should be able to print the grid formatted in the Telerik Report.  I am to the point where I can pass the grid to the new report / report viewer but here is where I am stuck.

How do I associate the columns and rows in the passed in gridview to fields on the report.  I would like to print 4-5 lines per detail record and I can create the appropriate fields but I can't find any information on how to bind the columns (cells) in the gridview to the fields I have created on the report.

I should also mention that the original source of the data in the grid is from an XML file.  (http://www.telerik.com/community/forums/winforms/gridview/populate-radgrid-with-hierarchical-xml-data.aspx)

My goal is to print the XML file response formatted in a Telerik Report.

Thoughts?

Doug

0
Martin Vasilev
Telerik team
answered on 28 Jul 2011, 04:10 PM
Hi Doug,

Thank you for writing.

Indeed, we do not support RadGridReportingLite anymore and it is possible that it could not work properly with Q2 2011 release. Generally speaking, there is not a straight way for binding fields between RadGridView and Telerik Reporting.

If you need to print the grid, I would recommend considering one of the built-in export features (to excel or pdf) and then print the grid from the opened file in the external program. 

Let me know if you have any other questions.

Regards,
Martin Vasilev
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Doug
Top achievements
Rank 2
answered on 15 Aug 2011, 02:29 PM
Thanks Martin,

I have been playing around with that and it may work for what I need to do, however, I can't get it to format very well.  I would like to be able to control the width of the columns that print on the page.  I have been able to get it to print landscape which is a requirement but I need to be able to set the column sizes on the parent row as well as the child rows.  (It is a grid with Hierachical data).  

HTMLCellFormatting seems like it might be the answer but I am unclear as to how to implement that when it is generating the "report".  Is there some sample code which may show me how to accomlish this?

UPDATE: I noticed in some other posts that another issue I am having with export to pdf is the overlap of data in the final columns because of a limitation with the pdf writer.  So after more thought, I would not be opposed to exporting to HTML and opening up the html file in a webbrowser control in my WINFORMS project.  So if there is a tutorial on export to html using the radgridview in Winforms, that would be most helpful.

Thanks for your help!

Doug

0
Martin Vasilev
Telerik team
answered on 18 Aug 2011, 12:37 PM
Hello Doug,

Thank you for contacting me again.

I suppose you are trying to use the ExportToPDF functionality. Actually, there is not a way to manually setup the column width while exporting. This cannot be done through HTMLCellFormatting event. However, the size is transferred from the grid's settings, which means you can change the column width before exporting and than set it back to the initial value. But you should keep in mind that the child column width in the exported file cannot be separately controlled. It takes the size of the upper parent column.

I confirm that there is an issue with overlapping data in some scenarios. You can work around it by using the ExportToPDF's Scale property, which will allow for sizing the exported grid according to the available page size. 

For detailed information on how to export to html format, you can refer in our product documentation.

Do not hesitate to contact me again if you have any additional questions.

Regards,
Martin Vasilev
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Doug
Top achievements
Rank 2
answered on 18 Aug 2011, 04:38 PM

Hi Martin,

Thank you for getting back to me!  I decided to switch from the ExportToPDF to ExportoHTML as there was no real need to use the PDF format of the file.  I am using both techniques you have outlined in various responses where I am formatting the grid to be pretty close to what I need and then formatting the HTML result before exporting it to a file.  I am 99% there.  Once the file is generated I am using the showpreviewdialog for the webbrowser control on my winform so that the user can preview the html page before printing.  This allows them to switch to Landscape layout to then do the print.  The 1% that I am missing is the ability to switch the layout for printing to landscape so I can send directly to the printer after viewing the grid taking the guess work away from the user.  If you have any thoughts on that, great, otherwise, thanks for all your help!

In case this helps anybody else:

Here is my code to load the grid from an XML file that is being returned to me:

if (xmlDataSet.Tables.Contains("PISearch") == true)
{
    this.uigrdSearchResults.DataSource = xmlDataSet.Tables[2];
    uigrdSearchResults.Columns[0].Width = 50;
    uigrdSearchResults.Columns[2].IsVisible = false;
    uigrdSearchResults.Columns[3].IsVisible = false;
    //=============================================
    // setup the grid and load using the xml data provided
    //=============================================
    if (xmlDataSet.Tables.Contains("PIRecord") == true)
    {
        GridViewTemplate PIResultsTemplate = new GridViewTemplate();
        PIResultsTemplate.AllowAddNewRow = false;
        PIResultsTemplate.AllowEditRow = true;
        var PIcolumn = new GridViewCheckBoxColumn();
        PIcolumn.HeaderText = "Select";
        PIcolumn.ReadOnly = false;
        PIResultsTemplate.Columns.Add(PIcolumn);
        this.uigrdSearchResults.MasterTemplate.Templates.Add(PIResultsTemplate);
        GridViewRelation relation = new GridViewRelation(this.uigrdSearchResults.MasterTemplate);
        relation.ChildTemplate = PIResultsTemplate;
        relation.RelationName = "PISearch_PIRecords";
        relation.ParentColumnNames.Add("PISearch_Id");
        relation.ChildColumnNames.Add("PIRecords_Id");
        this.uigrdSearchResults.Relations.Add(relation);
        PIResultsTemplate.DataSource = xmlDataSet.Tables[4];
        PIResultsTemplate.Columns[6].IsVisible = false; //Unit
        PIResultsTemplate.Columns[8].IsVisible = false; //Exempt Code
        PIResultsTemplate.Columns[16].IsVisible = false; //UID
        PIResultsTemplate.Columns[17].IsVisible = false; //PIRecord Index
        this.uigrdSearchResults.MasterTemplate.Templates[0].BestFitColumns();
    }
    this.uigrdSearchResults.MasterTemplate.BestFitColumns();
    this.uigrdSearchResults.AutoExpandGroups = true;
}

And my code to format the data to send to the HTML file  (ignore the void name, it is really html and not pdf :) ):

private void uibtnPrintToPDF_Click(object sender, EventArgs e)
{
    foreach (var row in this.uigrdSearchResults.Rows)
    {
        pitotrecords += row.ChildRows.Count;
        foreach (var childrow in row.ChildRows)
        {
            if (Convert.ToBoolean(childrow.Cells[0].Value) == false)
            {
                childrow.Delete();
                pichanged = true;
            }
            else
            {
                childrow.Cells[0].ColumnInfo.IsVisible = false// select column
                childrow.Cells[1].ColumnInfo.IsVisible = false// image flag column
                childrow.Cells[15].ColumnInfo.IsVisible = false; // PIRecord ID Column
            }
        }
        pirecords += row.ChildRows.Count;
    }
    foreach (var row in this.uigrdSearchResultsGI.Rows)
    {
        gitotrecords += row.ChildRows.Count;
        foreach (var childrow in row.ChildRows)
        {
            if (Convert.ToBoolean(childrow.Cells[0].Value) == false)
            {
                childrow.Delete();
                gichanged = true;
            }
            else
            {
                childrow.Cells[0].ColumnInfo.IsVisible = false// select column
                childrow.Cells[1].ColumnInfo.IsVisible = false// image flag column
                childrow.Cells[12].ColumnInfo.IsVisible = false// GUID
                childrow.Cells[13].ColumnInfo.IsVisible = false// GIRecord ID
            }
        }
        girecords += row.ChildRows.Count;
    }
    ExportToHTML PIexporter = new ExportToHTML(uigrdSearchResults);
    PIexporter.HiddenColumnOption = Telerik.WinControls.UI.Export.HiddenOption.DoNotExport;
    PIexporter.SummariesExportOption = SummariesOption.DoNotExport;
    PIexporter.Scale = 1;
    PIexporter.FileExtension = "html";
    PIexporter.TableBorderThickness = 1;
    PIexporter.TableCaption = "Order Search Results - PI";
    PIexporter.HTMLCellFormatting += new Telerik.WinControls.UI.Export.HTML.HTMLCellFormattingEventHandler(PIexporter_HTMLCellFormatting);
    PIexporter.HTMLTableCaptionFormatting += new Telerik.WinControls.UI.Export.HTML.HTMLTableCaptionFormattingEventHandler(PIexporter_HTMLTableCaptionFormatting);
    PIexporter.ExportVisualSettings = false;
    PIexporter.ExportHierarchy = true;
    PIexporter.RunExport("c:\\PISearchResults.html");
    ExportToHTML GIexporter = new ExportToHTML(uigrdSearchResultsGI);
    GIexporter.HiddenColumnOption = Telerik.WinControls.UI.Export.HiddenOption.DoNotExport;
    GIexporter.SummariesExportOption = SummariesOption.DoNotExport;
    GIexporter.Scale = 1;
    GIexporter.FileExtension = "html";
    GIexporter.TableBorderThickness = 1;
    GIexporter.TableCaption = "Order Search Results - GI";
    GIexporter.HTMLCellFormatting += new Telerik.WinControls.UI.Export.HTML.HTMLCellFormattingEventHandler(GIexporter_HTMLCellFormatting);
    GIexporter.HTMLTableCaptionFormatting += new Telerik.WinControls.UI.Export.HTML.HTMLTableCaptionFormattingEventHandler(GIexporter_HTMLTableCaptionFormatting);
    GIexporter.ExportVisualSettings = false;
    GIexporter.ExportHierarchy = true;
    GIexporter.RunExport("c:\\GISearchResults.html");
        webBrowser1.Url = new Uri("c:\\PISearchResults.html");
        webBrowser2.Url = new Uri("c:\\GISearchResults.html");
        Application.DoEvents();
        webBrowser1.ShowPrintPreviewDialog();
        webBrowser2.ShowPrintPreviewDialog();
        Application.DoEvents();
}
private void PIexporter_HTMLCellFormatting(object sender, Telerik.WinControls.UI.Export.HTML.HTMLCellFormattingEventArgs e)
{
    if (e.GridColumnIndex == 1 && e.GridRowInfoType == typeof(GridViewTableHeaderRowInfo))
    {
        e.HTMLCellElement.Colspan = 12;
    }
    if (e.GridColumnIndex == 1 && e.GridRowInfoType == typeof(GridViewHierarchyRowInfo))
    {
        e.HTMLCellElement.Colspan = 12;
    }
    if (e.HTMLCellElement.Value == "")
    {
        e.HTMLCellElement.Value = " ";
    }
    switch (e.HTMLCellElement.Value)
    {
        case "Blk":
            e.HTMLCellElement.Styles.Add("width", "20px");
            break;
        case "Lot":
            e.HTMLCellElement.Styles.Add("width", "20px");
            break;
        case "DocumentDate":
            e.HTMLCellElement.Styles.Add("width", "100px");
            break;
        case "InstrumentDate":
            e.HTMLCellElement.Styles.Add("width", "100px");
            break;
        case "Amount":
            e.HTMLCellElement.Styles.Add("width", "100px");
            break;
        default:
            e.HTMLCellElement.Styles.Add("width", "0px");
            break;
    }
}
private void PIexporter_HTMLTableCaptionFormatting(object sender, Telerik.WinControls.UI.Export.HTML.HTMLTableCaptionFormattingEventArgs e)
{
    e.TableCaptionElement.Styles.Add("background-color", ColorTranslator.ToHtml(Color.White));
    e.TableCaptionElement.Styles.Add("font-size", "100%");
    e.TableCaptionElement.Styles.Add("color", ColorTranslator.ToHtml(Color.Black));
    e.TableCaptionElement.Styles.Add("font-weight", "bold");
    e.CaptionText = "Order Search Results - PI: " + pirecords.ToString() + " of " + pitotrecords.ToString() + " selected";
}
private void GIexporter_HTMLCellFormatting(object sender, Telerik.WinControls.UI.Export.HTML.HTMLCellFormattingEventArgs e)
{
    if (e.GridColumnIndex == 1 && e.GridRowInfoType == typeof(GridViewTableHeaderRowInfo))
    {
        e.HTMLCellElement.Colspan = 10;
    }
    if (e.GridColumnIndex == 1 && e.GridRowInfoType == typeof(GridViewHierarchyRowInfo))
    {
        e.HTMLCellElement.Colspan = 10;
    }
    if (e.HTMLCellElement.Value == "")
    {
        e.HTMLCellElement.Value = " ";
    }
    switch (e.HTMLCellElement.Value)
    {
        case "PrincipalType":
            e.HTMLCellElement.Styles.Add("width", "50px");
            break;
        default:
            e.HTMLCellElement.Styles.Add("width", "0px");
            break;
    }
}
private void GIexporter_HTMLTableCaptionFormatting(object sender, Telerik.WinControls.UI.Export.HTML.HTMLTableCaptionFormattingEventArgs e)
{
    e.TableCaptionElement.Styles.Add("background-color", ColorTranslator.ToHtml(Color.White));
    e.TableCaptionElement.Styles.Add("font-size", "100%");
    e.TableCaptionElement.Styles.Add("color", ColorTranslator.ToHtml(Color.Black));
    e.TableCaptionElement.Styles.Add("font-weight", "bold");
    e.CaptionText = "Order Search Results - GI: " + girecords.ToString() + " of " + gitotrecords.ToString() + " selected";
}


Doug

0
Martin Vasilev
Telerik team
answered on 23 Aug 2011, 02:26 PM
Hi Doug,

Thank you very much for sharing your code. 

You have chosen an interesting approach to print RadGridView, which I am sure some other users could find useful. Unfortunately, I do not believe there is an elegant way to make the landscape default page orientation when bringing the print preview for WebBrowser control. As a kind of work-around you can use SendKeys class to simulate Alt+ L combination, which sets the preview to landscape:
this.webBrowser1.ShowPrintPreviewDialog();
Timer timer = new Timer();
timer.Interval = 400;
timer.Start();
timer.Tick += (s, args) =>
    {
        SendKeys.SendWait("%L");
        timer.Stop();
    };

I have updated your telerik points for posting your code in our community forums. Do not hesitate to write me back if you need any additional assistance.

Regards,
Martin Vasilev
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
GridView
Asked by
Dra Graca Melo
Top achievements
Rank 1
Answers by
Jack
Telerik team
Dra Graca Melo
Top achievements
Rank 1
Martin Vasilev
Telerik team
Doug
Top achievements
Rank 2
Share this question
or