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;
this
.radGridView1.PrintStyle.PrintHeaderOnEachPage =
true
;
this
.radGridView1.PrintStyle.PrintHiddenRows =
false
;
this
.radGridView1.PrintStyle.PrintGrouping =
true
;
this
.radGridView1.PrintStyle.PrintSummaries =
true
;
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;
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;
}
}
}
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);
}
else
{
e.Graphics.FillEllipse(Brushes.Red, circleRect);
}
e.Graphics.SmoothingMode = smoothingMode;
}
}
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!
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.