Click here to Skip to main content
15,892,059 members
Articles / Programming Languages / Visual Basic

Couluring and centering cells in a DataGridView

Rate me:
Please Sign up or sign in to vote.
2.13/5 (9 votes)
20 Jan 2007CPOL 39.1K   21   8
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):

VB
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!!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Other
Ireland Ireland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Global Analyser3-Nov-10 6:56
Global Analyser3-Nov-10 6:56 
good i like that u can made it better
Questionrows.. [modified] Pin
brunili5-Jul-08 19:31
brunili5-Jul-08 19:31 
AnswerRe: rows.. Pin
DA_Loring6-Jul-08 23:14
DA_Loring6-Jul-08 23:14 
QuestionCan you give an example? Pin
Happyman25425-Jul-07 18:04
Happyman25425-Jul-07 18:04 
Questionmint_AvailCapCol ?? Pin
changdol8-Mar-07 20:53
changdol8-Mar-07 20:53 
AnswerRe: mint_AvailCapCol ?? Pin
DA_Loring8-Mar-07 22:14
DA_Loring8-Mar-07 22:14 
QuestionI dont get this event Pin
christefer7-Mar-07 0:49
christefer7-Mar-07 0:49 
AnswerRe: I dont get this event Pin
DA_Loring7-Mar-07 21:18
DA_Loring7-Mar-07 21:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.