Painting on the DataGridView

Posted Thu, Aug 13 2009 14:33 by Deborah Kurata

If you have a data in a Windows Forms DataGridView, you may want to use some color to highlight specific rows of the grid to draw in the user’s attention.

In this example, the code paints a border around a specific row and sets the background color.

In C#:

private void myGrid_RowPostPaint(object sender,
               DataGridViewRowPostPaintEventArgs e)
{
    if (e.RowIndex == 2)
    {
        // Calculate the bounds of the row
        int rowHeaderWidth = myGrid.RowHeadersVisible ?
                             myGrid.RowHeadersWidth : 0;
        Rectangle rowBounds = new Rectangle(
            rowHeaderWidth,
            e.RowBounds.Top,
            myGrid.Columns.GetColumnsWidth(
                    DataGridViewElementStates.Visible) -
                    myGrid.HorizontalScrollingOffset + 1, 
           e.RowBounds.Height);

        // Paint the border
        ControlPaint.DrawBorder(e.Graphics, rowBounds, 
                         Color.Red, ButtonBorderStyle.Solid);

        // Paint the background color
        myGrid.Rows[e.RowIndex].DefaultCellStyle.BackColor =
                                             Color.BlanchedAlmond;
    }
}

In VB:

Private Sub myGrid_RowPostPaint(ByVal sender As Object, _
        ByVal e As DataGridViewRowPostPaintEventArgs)
        Handles myGrid.RowPostPaint
    If e.RowIndex = 2 Then
        ' Calculate the bounds of the row
        Dim rowHeaderWidth As Integer = _
                If(myGrid.RowHeadersVisible,  _
                   myGrid.RowHeadersWidth, 0)
        Dim rowBounds As New Rectangle( _
            rowHeaderWidth, _
            e.RowBounds.Top, _
            myGrid.Columns.GetColumnsWidth( _
                    DataGridViewElementStates.Visible) - _
                    myGrid.HorizontalScrollingOffset + 1, _
            e.RowBounds.Height)

        ' Paint the border
        ControlPaint.DrawBorder(e.Graphics, rowBounds, _
                         Color.Red, ButtonBorderStyle.Solid)

        ' Paint the background color
        myGrid.Rows(e.RowIndex).DefaultCellStyle.BackColor = _
                                        Color.BlanchedAlmond
    End If

End Sub

In both examples, this code hard-codes the highlighted row to the 3rd row (0-based). You can use any criteria that makes sense for your application. For example, any row with a date after a specific date or with a quantity greater than a specific amount.

The code then determines whether or not the grid displays row headers. If the headers are displayed, the start location of the border will be after the row header. Otherwise, the border will start at 0.

The next step is to determine the bounds of the border. This is done by creating a rectangle with the appropriate upper left corner (x and y location), width and height.

The x location depends on whether the row headers are displayed. In this example the border did not include the row headers. The y location is the top of the underlying row. The rectangle width uses the GetColumnsWidth method to ensure only visible columns widths are included in calculating the rectangle width. The rectangle height is the height of the underlying row.

The DrawBorder method is used to draw a red border around the created rectangle. You can select any color or border style you desire.

To draw more attention to the row, the background color is also changed. This step is optional if the border provides enough notice for your application.

The resulting grid appears as follows:

image

If the grid has row headers, it will appear like this:

image

Notice that the row header is not included in the border.

Enjoy!

Filed under: , , , ,

Comments

# Painting on the DataGridView - Deborah Kurata

Friday, August 14, 2009 2:01 PM by DotNetShoutout

Thank you for submitting this cool story - Trackback from DotNetShoutout

# re: Painting on the DataGridView

Friday, September 04, 2009 4:53 AM by John Braga

Thank you for the above code.  Works very well, except for one small error in the c# code.  You set an integer rowHeaderWidth which will be 0 if row headers are NOT visible, but then you do not use it when setting the rectangle bounds, so the rectangle is offset.

I think rowHeaderswidth in the VB example should be rowHeaderwidth, but have not tested it.

# re: Painting on the DataGridView

Friday, September 04, 2009 10:36 AM by Deborah Kurata

Hi John -

Thank you for catching that! I corrected the code in the post.

Thanks again -

# re: Painting on the DataGridView

Tuesday, February 23, 2010 4:35 PM by Luis Marquez

Is there a way of doing that but with every cells of the datagridview?

# re: Painting on the DataGridView

Tuesday, February 23, 2010 7:39 PM by Deborah Kurata

Hi Luis -

If you mean setting the grid line color, you can set it using the GridColor property of the DataGridView.

Hope this helps.

# re: Painting on the DataGridView

Tuesday, February 23, 2010 10:24 PM by Luis Marquez

Thanks for the fast reply but I Already Tried. The problem that I have is that I need that the lines that divide columns have to be more thicker and the way that you paint a row is basically what I need but on a column.

# re: Painting on the DataGridView

Wednesday, February 24, 2010 10:13 AM by Deborah Kurata

Hi Luis -

You should be able to tailor this technique to paint a column as well. Try modifying the Rectangle code to draw the rectangle at different coordinates. If you try it and still need help, you can post questions to:

social.msdn.microsoft.com/.../windowsforms

Also, this is *lots* easier in WPF or Silverlight if that is an option for you.

Hope this helps.

# re: Painting on the DataGridView

Wednesday, February 24, 2010 11:35 AM by Luis Marquez

Thanks for the help

# re: Painting on the DataGridView

Friday, April 23, 2010 3:41 PM by Richard

Exactly what I was looking for! Thanks for posting it.

# re: Painting on the DataGridView

Friday, January 07, 2011 8:22 AM by David Logsdon aka Frazzle

very close to what i was looking for except the border and different color for each row.  I have played around with your code and cant seem to get it working.

David

If i seem Confused Well I am

# re: Painting on the DataGridView

Friday, January 07, 2011 2:32 PM by Deborah Kurata

Hi David -

Could you be more specific on the problem you are having with the code? If it is easier, you can post your question with the relevant parts of the code on the forums: social.msdn.microsoft.com/.../categories

Then put the link to your question in reply to this blog post. By posting  on the forums, we can more easily look at your code and provide for others to help.

Thanks!

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: