Couluring and centering cells in a DataGridView
Describes a method of colouring and centering cells in a DataGridView.
Introduction
I had many problems trying to find a method for colouring and centering cells on a DataGridView
. I managed to find a way to do it and I thought I would share it. In my application, there are three colours and this block of code, so I have included the whole code. Hope this helps someone.
Include this code in the CellPainting
event... (the name of the grid in this instance is dgvAvail
):
Private Sub dgvAvail_CellPainting(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) _
Handles dgvAvail.CellPainting
If Me.dgvAvail.Columns(mint_AvailCapCol).Index = _
e.ColumnIndex AndAlso e.RowIndex >= 0 Then
Dim newRect As New Rectangle(e.CellBounds.X + 1, e.CellBounds.Y + 1, _
e.CellBounds.Width - 4, e.CellBounds.Height - 4)
Dim redBackBrush As New SolidBrush(Color.Red)
Dim orangeBackBrush As New SolidBrush(Color.FromArgb(242, 181, 38))
Dim greenBackBrush As New SolidBrush(Color.LightGreen)
Dim gridBrush As New SolidBrush(Me.dgvAvail.GridColor)
Dim gridLinePen As New Pen(gridBrush)
Dim nsCellValue As Single = 0
Dim nsMaxValue As Single = 0
Dim sf As New StringFormat
sf.FormatFlags = StringFormatFlags.NoWrap Or StringFormatFlags.FitBlackBox
sf.LineAlignment = StringAlignment.Center
sf.Alignment = StringAlignment.Center
sf.Trimming = StringTrimming.None
If Information.IsDBNull(Me.dgvAvail.Rows(e.RowIndex).Cells(_
mint_AvailCapCol).Value) = True Then
nsCellValue = 0
Else
nsCellValue = _
Me.dgvAvail.Rows(e.RowIndex).Cells(mint_AvailCapCol).Value
End If
If Information.IsDBNull(Me.dgvAvail.Rows(_
e.RowIndex).Cells(mint_MaxCapCol).Value) = True Then
nsMaxValue = 0
Else
nsMaxValue = _
Me.dgvAvail.Rows(e.RowIndex).Cells(mint_MaxCapCol).Value
End If
Try
If nsCellValue = 0 Then
'This is a full outage and is shown Red
' Erase the cell.
e.Graphics.FillRectangle(redBackBrush, e.CellBounds)
' Draw the grid lines (only the right and bottom lines;
' DataGridView takes care of the others).
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, _
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, _
e.CellBounds.Bottom - 1)
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, _
e.CellBounds.Top, e.CellBounds.Right - 1, _
e.CellBounds.Bottom)
' Draw the text content of the cell, ignoring alignment.
If Not (e.Value Is Nothing) Then
Dim r As System.Drawing.Rectangle
r.Width = (e.CellBounds.Right - 2) - (e.CellBounds.Left + 2)
r.Height = (e.CellBounds.Bottom - 2) - (e.CellBounds.Top + 2)
r.X = e.CellBounds.X + 2
r.Y = e.CellBounds.Y + 2
e.Graphics.TextRenderingHint = _
Drawing.Text.TextRenderingHint.AntiAliasGridFit
e.Graphics.DrawString(CStr(nsCellValue), _
e.CellStyle.Font, Brushes.Black, r, sf)
r = Nothing
End If
ElseIf nsCellValue > 0 And nsCellValue < nsMaxValue _
- (nsMaxValue * gCST_OrangeBoundaryValue / 100) Then
'This is a partial outage and is shown Orange
' Erase the cell.
e.Graphics.FillRectangle(orangeBackBrush, e.CellBounds)
' Draw the grid lines (only the right and bottom lines;
' DataGridView takes care of the others).
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, _
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, _
e.CellBounds.Bottom - 1)
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, _
e.CellBounds.Top, e.CellBounds.Right - 1, _
e.CellBounds.Bottom)
' Draw the text content of the cell, ignoring alignment.
If Not (e.Value Is Nothing) Then
Dim r As System.Drawing.Rectangle
r.Width = (e.CellBounds.Right - 2) - (e.CellBounds.Left + 2)
r.Height = (e.CellBounds.Bottom - 2) - (e.CellBounds.Top + 2)
r.X = e.CellBounds.X + 2
r.Y = e.CellBounds.Y + 2
e.Graphics.TextRenderingHint = _
Drawing.Text.TextRenderingHint.AntiAliasGridFit
e.Graphics.DrawString(CStr(nsCellValue), _
e.CellStyle.Font, Brushes.Black, r, sf)
r = Nothing
End If
Else
'This is an OK situation and is shown Green
' Erase the cell.
e.Graphics.FillRectangle(greenBackBrush, e.CellBounds)
' Draw the grid lines (only the right and bottom lines;
' DataGridView takes care of the others).
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, _
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, _
e.CellBounds.Bottom - 1)
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, _
e.CellBounds.Top, e.CellBounds.Right - 1, _
e.CellBounds.Bottom)
' Draw the text content of the cell, ignoring alignment.
If Not (e.Value Is Nothing) Then
Dim r As System.Drawing.Rectangle
r.Width = (e.CellBounds.Right - 2) - (e.CellBounds.Left + 2)
r.Height = (e.CellBounds.Bottom - 2) - (e.CellBounds.Top + 2)
r.X = e.CellBounds.X + 2
r.Y = e.CellBounds.Y + 2
e.Graphics.TextRenderingHint = _
Drawing.Text.TextRenderingHint.AntiAliasGridFit
e.Graphics.DrawString(CStr(nsCellValue), _
e.CellStyle.Font, Brushes.Black, r, sf)
r = Nothing
End If
End If
e.Handled = True
Catch er As ApplicationException
MsgBox(er.Message)
Finally
gridLinePen.Dispose()
gridBrush.Dispose()
redBackBrush.Dispose()
greenBackBrush.Dispose()
orangeBackBrush.Dispose()
sf.Dispose()
End Try
End If
End Sub
Enjoy!!!