Printing GridView Custom Painting

5 posts, 0 answers
  1. Chosi
    Chosi avatar
    3 posts
    Member since:
    Jun 2016

    Posted 26 Jun Link to this post

    How can I print a radGridView with custom painting? See the attached image.
  2. Dimitar
    Admin
    Dimitar avatar
    1408 posts

    Posted 27 Jun Link to this post

    Hello Chosi,

    Thank you for writing.

    You can use the PrintCellPaint event and add similar code: Events and Customization.

    Let me know if I can assist you further.

    Regards,
    Dimitar
    Telerik
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Chosi
    Chosi avatar
    3 posts
    Member since:
    Jun 2016

    Posted 28 Jun in reply to Dimitar Link to this post

    Here is my RowPaint event:

    01.private void grid_RowPaint(object sender, GridViewRowPaintEventArgs e)
    02.        {
    03.            if (e.Row is GridDataRowElement)
    04.            {
    05.                GridDataRowElement rowElement = e.Row as GridDataRowElement;
    06. 
    07.                int start = (int)e.Row.RowInfo.Cells["StartCell"].Value;
    08.                int end = (int)e.Row.RowInfo.Cells["EndCell"].Value;
    09.                Color color = (Color)e.Row.RowInfo.Cells["Color"].Value;
    10. 
    11.                if (end == 0)
    12.                    return;
    13.                 
    14.                GridDataCellElement startCell = GetCell(rowElement, start);
    15.                GridDataCellElement endCell = GetCell(rowElement, end);
    16. 
    17.                if (startCell == null || endCell == null)
    18.                {
    19.                    return;
    20.                }
    21. 
    22.                int left = startCell.PointToControl(startCell.Bounds.Location).X + (startCell.TableElement.CellSpacing * startCell.ColumnIndex);
    23.                int right = endCell.PointToControl(new Point(endCell.Bounds.Right, endCell.Bounds.Bottom)).X;
    24. 
    25.                if (grid.RightToLeft == System.Windows.Forms.RightToLeft.Yes)
    26.                {
    27.                    left = endCell.PointToControl(startCell.Bounds.Location).X + (endCell.TableElement.CellSpacing * startCell.ColumnIndex) - (int)e.Graphics.Transform.OffsetX;
    28.                    right = startCell.PointToControl(new Point(startCell.Bounds.Right, startCell.Bounds.Bottom)).X - (int)e.Graphics.Transform.OffsetX;
    29.                }
    30. 
    31.                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
    32.                e.Graphics.SetClip(rowElement.ScrollableColumns.BoundingRectangle);
    33. 
    34.                using (RoundRectShape shape = new RoundRectShape(5))
    35.                {
    36.                    var rect = new RectangleF(left + 10, 5, (right - left) - 15, e.Row.Size.Height - 10);
    37. 
    38.                    using (GraphicsPath path = shape.CreatePath(rect))
    39.                    using (Brush brush = new SolidBrush(color))
    40.                    using (Pen pen = new Pen(Color.FromArgb(color.A, (int)(color.R * 0.5), (int)(color.G * 0.5), (int)(color.B * 0.5)), 2))
    41.                    {
    42.                        e.Graphics.FillPath(brush, path);
    43.                        e.Graphics.DrawPath(pen, path);
    44.                    }
    45.                }
    46.            }
    47.        }
    48. 
    49.private GridDataCellElement GetCell(GridRowElement rowElement, int phaseId)
    50.        {
    51.            foreach (GridCellElement cell in rowElement.VisualCells)
    52.            {
    53.                if (cell.ColumnInfo != null && cell.ColumnInfo.Name == phaseId.ToString())
    54.                {
    55.                    return cell as GridDataCellElement;
    56.                }
    57.            }
    58.            return null;
    59.        }

    How should I use this code in PrintCellPaint event?

  5. Chosi
    Chosi avatar
    3 posts
    Member since:
    Jun 2016

    Posted 28 Jun Link to this post

    As you can see, I have a line as bellow:

    GridDataRowElement rowElement = e.Row as GridDataRowElement;

    But I cannot use this code in PrintCellPrint event.

  6. Dimitar
    Admin
    Dimitar avatar
    1408 posts

    Posted 28 Jun Link to this post

    Hello Chosi,

    Thank you for writing back.

    You do not need the row element in this case. You need to know where the shape starts and ends. For example:
    private void RadGridView1_PrintCellPaint(object sender, PrintCellPaintEventArgs e)
    {
        GridViewDataRowInfo dataRow = e.Row as GridViewDataRowInfo;
        if (dataRow != null)
        {
            bool isStartCell = e.Column.Index == 0;
            bool isEndCell = e.Column.Index == 4;
            bool isMiddle = e.Column.Index > 0 && e.Column.Index < 4;
     
            var color = Color.Red;
     
            if (isStartCell)
            {
                using (RoundRectShape shape = new RoundRectShape(5, true, true, false, false))
                {
                    var rect = new Rectangle(e.CellRect.X + 3, e.CellRect.Y + 3, e.CellRect.Width - 3, e.CellRect.Height - 6);
     
                    using (GraphicsPath path = shape.CreatePath(rect))
                    using (Brush brush = new SolidBrush(color))
                    using (Pen pen = new Pen(Color.FromArgb(color.A, (int)(color.R * 0.5), (int)(color.G * 0.5), (int)(color.B * 0.5)), 2))
                    {
                        e.Graphics.FillPath(brush, path);
                        e.Graphics.DrawPath(pen, path);
                    }
                }
            }
     
            if (isEndCell)
            {
                using (RoundRectShape shape = new RoundRectShape(5, false, false, true, true))
                {
                    var rect = new Rectangle(e.CellRect.X , e.CellRect.Y + 3, e.CellRect.Width - 3, e.CellRect.Height - 6);
     
                    using (GraphicsPath path = shape.CreatePath(rect))
                    using (Brush brush = new SolidBrush(color))
                    using (Pen pen = new Pen(Color.FromArgb(color.A, (int)(color.R * 0.5), (int)(color.G * 0.5), (int)(color.B * 0.5)), 2))
                    {
                        e.Graphics.FillPath(brush, path);
                        e.Graphics.DrawPath(pen, path);
                        e.Graphics.FillRectangle(new SolidBrush(color),e.CellRect.X -2, e.CellRect.Y + 4,4,e.CellRect.Height - 8);
                    }
                }
            }
     
            if (isMiddle)
            {
                using (RoundRectShape shape = new RoundRectShape(5, false, false, false, false))
                {
                    var rect = new Rectangle(e.CellRect.X -1, e.CellRect.Y + 3, e.CellRect.Width+2, e.CellRect.Height - 6);
     
                    using (GraphicsPath path = shape.CreatePath(rect))
                    using (Brush brush = new SolidBrush(color))
                    using (Pen pen = new Pen(Color.FromArgb(color.A, (int)(color.R * 0.5), (int)(color.G * 0.5), (int)(color.B * 0.5)), 2))
                    {
     
                        e.Graphics.FillPath(brush, path);
                        e.Graphics.DrawLine(pen, new Point(e.CellRect.X, e.CellRect.Y + 3), new Point(e.CellRect.X + e.CellRect.Width, e.CellRect.Y+3));
                        e.Graphics.DrawLine(pen, new Point(e.CellRect.X, e.CellRect.Y +e.CellRect.Height - 3), new Point(e.CellRect.X + e.CellRect.Width, e.CellRect.Y+ e.CellRect.Height-3));
                    }
                }
            }
        }
    }

    I hope this will be useful. 

    Regards,
    Dimitar
    Telerik
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
Back to Top
UI for WinForms is Visual Studio 2017 Ready