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:
If the grid has row headers, it will appear like this:
Notice that the row header is not included in the border.
Enjoy!