New to Telerik UI for WinForms? Download free 30-day trial

Painting and drawing in cells

There are cases when you need to manually draw in a cell in order to extend the cell appearance and/or provide additional information to the user about the cell data. RadGridView supports this case via the CellPaint event. To enable the event firing just set EnableCustomDrawing to true.

When handling this event, you should access the cell through the parameters of the event handler, rather than access the cell directly.

The following example demonstrates how to use the CellPaint event to change the appearance of the cells in a "UnitPrice" column. We would like to add a custom drawn red asterisk to values less than 20, a green asterisk to values higher than 20, and no asterisk when the cell's value is zero:

void radGridView1_CellPaint(object sender, Telerik.WinControls.UI.GridViewCellPaintEventArgs e)
{
    if (e.Cell != null && e.Cell.RowInfo is GridViewDataRowInfo && e.Cell.ColumnInfo.Name == "UnitPrice")
    {
        double value = Convert.ToDouble(e.Cell.Value);
        if (value == 0)
        {
            return;
        }
        Brush brush = value < 20 ? Brushes.Red : Brushes.Green;
        using (Font font = new Font("Segoe UI", 17))
        {
            e.Graphics.DrawString("*", font, brush, Point.Empty);
        }
    }
}

Private Sub RadGridView1_CellPaint(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCellPaintEventArgs) Handles RadGridView1.CellPaint
    If e.Cell IsNot Nothing AndAlso TypeOf e.Cell.RowInfo Is GridViewDataRowInfo AndAlso e.Cell.ColumnInfo.Name = "UnitPrice" Then
        Dim value As Double = Convert.ToDouble(e.Cell.Value)
        If value = 0 Then
            Return
        End If
        Dim brush As Brush = If(value < 20, Brushes.Red, Brushes.Green)
        Using font As New Font("Segoe UI", 17)
            e.Graphics.DrawString("*", font, brush, Point.Empty)
        End Using
    End If

Figure 1: Painting in cells

WinForms RadGridView Painting in cells

See Also

In this article