We are sure you will agree that the highly demanded Printing support for RadGridView for WinForms was welcomed warmly by your end-users. We wanted to make sure that the Print Preview, Print Settings and Watermark dialogs will make the printing as easy as possible, but at the same time they will enable end-users to customize the printing output to the smallest detail. The result is that you can print a RadGridView with a call to the Print() method and the grid is send to the printer.

On the other hand if you want to customize the result by using code, you have a bunch of options available. On the higher level you can customize the RadPrintDocument which offers options for headers, footers, paper sizes, margins and the lot. But what we are here for is to dig deeper into the RadGridView print settings.

For this example, let’s take a look at the Employees table from the Northwind database. The first thing to note is that you can print all types of view definitions and you do not have to worry about it, the RadGridView will handle it for you. To get going, we first have to decide how the RadGridView will be laid out on the printed sheet of paper. Let`s set up our grid to be printed in the center without resizing it:

this.radGridView1.PrintStyle.FitWidthMode = PrintFitWidthMode.NoFitCentered;

Now we can choose whether to print the header cells on each page to make it easier to understand the data that is printed:
this.radGridView1.PrintStyle.PrintHeaderOnEachPage = true;

Next, we can choose which rows to print:
this.radGridView1.PrintStyle.PrintHiddenRows = false;
this.radGridView1.PrintStyle.PrintGrouping = true;
this.radGridView1.PrintStyle.PrintSummaries = true;

Now we can go for the visual appeal of the printed grid. There are options available for setting the fonts and back colors for each cell type e.g. header cells, group cells, summary cells. There is also an option to enable the alternating row color for the data rows:
this.radGridView1.PrintStyle.HeaderCellFont = new Font("Arial", 9, FontStyle.Bold);
this.radGridView1.PrintStyle.HeaderCellBackColor = Color.LightBlue;
this.radGridView1.PrintStyle.GroupRowFont = new Font("Helvetica", 10, FontStyle.Regular);
this.radGridView1.PrintStyle.GroupRowBackColor = Color.PaleGoldenrod;

So far we’ve looked at properties that modify settings on a type level for all representatives of that type, rather than modifying individual instances. In order to access each individual cell, you can use the PrintCellFormatting and PrintCellPaint events. In our case let’s say we want to set the cell back color which contains the “Hire date” for employees that have been in the company for 20 years or more:
private void radGridView1_PrintCellFormatting(object sender, PrintCellFormattingEventArgs e)
    if (e.Row is GridViewDataRowInfo && e.Column.Name == "HireDate")
        DateTime date = (DateTime)e.Row.Cells[e.Column.Name].Value;
        if (date.Year + 20 <= DateTime.Now.Year)
            e.PrintCell.DrawFill = true;
            e.PrintCell.BackColor = Color.LightGray;

Finally, let`s put a small circle in the “Date of birth” cell which indicates whether the given employee birthday has passed or is up to come:
private void radGridView1_PrintCellPaint(object sender, PrintCellPaintEventArgs e)
    if (e.Row.Cells[e.Column.Name] != null && e.Row is GridViewDataRowInfo && e. Column.Name == "BirthDate")
        SmoothingMode smoothingMode = e.Graphics.SmoothingMode;
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        DateTime date = (DateTime)e.Row.Cells[e.Column.Name].Value;
        int diameter = e.CellRect.Height / 2;
        int circleRectX = e.CellRect.X + e.CellRect.Width - diameter - (diameter / 2);
        int circleRectY = e.CellRect.Y + diameter / 2;
        Rectangle circleRect = new Rectangle(circleRectX, circleRectY, diameter, diameter);
        if (date.DayOfYear >= DateTime.Now.DayOfYear)
            e.Graphics.FillEllipse(Brushes.Green, circleRect);
            e.Graphics.FillEllipse(Brushes.Red, circleRect);
        e.Graphics.SmoothingMode = smoothingMode;

Voala! This is it, now let`s preview the print job, before we print:

RadScheduler for WinForms Printing for WinForms

For more information about the printing support and its tweaks, you can check our brand new video Getting Started with Printing for RadGridView.

Let the printing begin!

About the Author

Nikolay Diyanov

Diyanov is the Product Manager of the Native Mobile UI division at Progress. Delivering outstanding solutions that make developers' lives easier is his passion and the biggest reward in his work. In his spare time, Nikolay enjoys travelling around the world, hiking, sun-bathing and kite-surfing.

Find him on Twitter @n_diyanov or on LinkedIn.

Related Posts


Comments are disabled in preview mode.