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

Custom RowPaint does not work in Print event

7 Answers 21 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Rodrigo Cesar
Top achievements
Rank 1
Rodrigo Cesar asked on 20 Jul 2018, 05:13 PM

I'm using RowPaint event to "merge" cells. But when Printing it is not using the RowPaint event.

I tried to use PrintCellPaint event. But Row of event args are different. One is GridDataRowElement and other is GridViewDataRowInfo.

How can I use the same code to both events?

private void radGridView2_RowPaint(object sender, GridViewRowPaintEventArgs e)
{
    var dataRow = e.Row as GridDataRowElement;
    if (dataRow == null)
        return;
 
    var value = Convert.ToInt32(dataRow.RowInfo.Cells["Group"].Value);
    if (value == -1)
        return;
 
    var startCell = e.Row.VisualCells[2];
 
    var leftPos = startCell.PointToControl(startCell.Bounds.Location).X + (startCell.TableElement.CellSpacing * startCell.ColumnIndex);
 
    var msgLocation = new Rectangle(leftPos + 1, 1, e.Row.Bounds.Width - leftPos - 2, e.Row.Bounds.Height - 2);
 
    e.Graphics.FillRectangle(new SolidBrush(Color.White), msgLocation);
    e.Graphics.DrawString(EventLogs[value], e.Row.Font, new SolidBrush(Color.Black), msgLocation);
 
    var textSize = e.Graphics.MeasureString(EventLogs[value], e.Row.Font, msgLocation.Width);
    //if (textSize.Height > dataRow.Size.Height)
        startCell.RowInfo.Height = (int)textSize.Height;
}

7 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 23 Jul 2018, 09:50 AM
Hello Rodrigo,

The RowPaint event allows modifications of how the elements are painted in the grid and it will not fire while printing the control. PrintCellPaintEventArgs do not expose the row element object as it is part of the virtualized visual tree building the control. The actual data row object is exposed and you perform customizations on how the cells will be painted, however, running the same code in both events would not be possible. Additional information is available here: https://docs.telerik.com/devtools/winforms/gridview/printing-support/events-and-customization.

I hope this helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Rodrigo Cesar
Top achievements
Rank 1
answered on 23 Jul 2018, 01:43 PM
Ok. But how can I do this "merge" cell in print? Is it possible in PrintCellPaint? (I don't understand why there isn't a PrintRowPaint event!)
0
Accepted
Hristo
Telerik team
answered on 24 Jul 2018, 08:46 AM
Hi Rodrigo,

The merged cells you have achieved are a result of special painting and such customizations are not supported by the printing engine. While printing, entire rows are not painted rather only the cells and this is done one cell at a time, hence there is no row paint nor row formatting event. Yet, you can create a custom print renderer and implement the PrintRow method according to your local setup: 
public class CustomTableViewDefinitionPrintRenderer : TableViewDefinitionPrintRenderer
{
    public CustomTableViewDefinitionPrintRenderer(RadGridView grid)
        : base(grid)
    {
    }
 
    protected override void PrintRow(GridViewRowInfo row, TableViewRowLayout rowLayout, GridPrintSettings settings, int currentX, int currentY, Graphics graphics, Rectangle drawArea)
    {
        base.PrintRow(row, rowLayout, settings, currentX, currentY, graphics, drawArea);
         
        //...
    }
}

Then you can use the rendered like this: 
this.radGridView1.PrintStyle.PrintRenderer = new CustomTableViewDefinitionPrintRenderer(this.radGridView1);

I hope this information is useful.

Regards,
Hristo
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Rodrigo Cesar
Top achievements
Rank 1
answered on 24 Jul 2018, 02:41 PM

Ok. That worked to me.

Follow my render code if anyone has interest in it.

public class CustomTableViewDefinitionPrintRenderer : TableViewDefinitionPrintRenderer
{
    private RadGridView _grid;
    public EventHandler<RowPrintArgs> RowPrint;
 
    public CustomTableViewDefinitionPrintRenderer(RadGridView grid)
        : base(grid)
    {
        _grid = grid;
    }
 
    protected override void PrintRow(GridViewRowInfo row, TableViewRowLayout rowLayout, GridPrintSettings settings, int currentX, int currentY, Graphics graphics, Rectangle drawArea)
    {
        base.PrintRow(row, rowLayout, settings, currentX, currentY, graphics, drawArea);
 
        if (RowPrint != null)
        {
            var args = new RowPrintArgs()
            {
                Row = row,
                RowLayout = rowLayout,
                PrintSettings = settings,
                CurrentPos = new Point(currentX, currentY),
                Graphics = graphics,
                DrawArea = drawArea
            };
 
            RowPrint(_grid, args);
        }
    }
}
 
public class RowPrintArgs : EventArgs
{
    public GridViewRowInfo Row { get; set; }
    public TableViewRowLayout RowLayout { get; set; }
    public GridPrintSettings PrintSettings { get; set; }
    public Point CurrentPos { get; set; }
    public Graphics Graphics { get; set; }
    public Rectangle DrawArea { get; set; }
}

 

And the use:

private void RowPrintEvent(object sender, RowPrintArgs e)
{
    if (!(e.Row.Cells["Group"].Value is int))
        return;
 
    var value = Convert.ToInt32(e.Row.Cells["Group"].Value);
    if (value == -1)
        return;
 
    var startCell = e.RowLayout.LayoutImpl.ArrangeInfos.First(x => x.Value.Column.FieldName.Equals("Id")).Value;
 
    var leftPos = startCell.OffsetX;
             
    var msgLocation = new RectangleF(e.CurrentPos.X + leftPos + 1, e.CurrentPos.Y, e.RowLayout.DesiredSize.Width - leftPos - 2, e.Row.Height);
    msgLocation.Offset(1, 1);
    msgLocation.Width--;
    msgLocation.Height--;
 
    e.Graphics.FillRectangle(new SolidBrush(Color.White), msgLocation);
    e.Graphics.DrawString(EventLogs[value], e.PrintSettings.CellFont, new SolidBrush(Color.Black), msgLocation);
}

 

But I have other question. There is a way to adjust row height in print view?

0
Hristo
Telerik team
answered on 25 Jul 2018, 06:20 AM
Hi Rodrigo,

Thank you for sharing your final solution with the community. I am also glad that I managed to help and the custom print renderer is working well in your project.

Regards,
Hristo
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Rodrigo Cesar
Top achievements
Rank 1
answered on 25 Jul 2018, 11:16 AM
Other question: There is a way to adjust row height in PrintRow function?
0
Hristo
Telerik team
answered on 26 Jul 2018, 09:46 AM
Hi Rodrigo,

The row`s height is generated according to the applied height of the actual row in the grid. It is extracted in the virtual GetDataRowHeight  method in the print renderer class. As you are already using a custom TableViewDefinitionPrintRenderer you can also override the GetDataRowHeight method and change it according to your actual scenario.

Regards,
Hristo
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
GridView
Asked by
Rodrigo Cesar
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Rodrigo Cesar
Top achievements
Rank 1
Share this question
or