Click here to Skip to main content
Licence CPOL
First Posted 20 Jan 2007
Views 25,553
Bookmarked 20 times

Couluring and centering cells in a DataGridView

By | 20 Jan 2007 | Article
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!!!

License

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

About the Author

DA_Loring

Other

Ireland Ireland

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberHeaven20206:56 3 Nov '10  
Questionrows.. [modified] Pinmemberbrunili19:31 5 Jul '08  
AnswerRe: rows.. PinmemberDA_Loring23:14 6 Jul '08  
QuestionCan you give an example? PinmemberHappyman25418:04 25 Jul '07  
Questionmint_AvailCapCol ?? Pinmemberchangdol20:53 8 Mar '07  
AnswerRe: mint_AvailCapCol ?? PinmemberDA_Loring22:14 8 Mar '07  
QuestionI dont get this event PinmemberAvdooth0:49 7 Mar '07  
AnswerRe: I dont get this event PinmemberDA_Loring21:18 7 Mar '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 20 Jan 2007
Article Copyright 2007 by DA_Loring
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid