65.9K
CodeProject is changing. Read more.
Home

FlagColumn Outlook Style

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.82/5 (5 votes)

Feb 4, 2007

CPOL
viewsIcon

37161

downloadIcon

555

Creating a DataGridViewFlagColumn in Outlook style.

Sample image

Introduction

The DataGridViewFlagColumn inherits the existing System.Windows.Forms.DataGridViewImageColumn. It paints the backcolor and chooses the image depending on the value you give the cell. In this code, I use a Flag enumeration of four possible values:

Public Enum Flag As Integer
    Blank = 0
    Red = 1
    Blue = 2
    Yellow = 3
End Enum 

If you create more flag icons, you can extend this enumeration. There also is an ErrorIcon supplied: this icon will be drawn if the value of the cell does not match one of the enumeration values.

The code has two classes: DataGridViewFlagColumn with just one member:

Public NotInheritable Class DataGridViewFlagColumn
    Inherits DataGridViewImageColumn

    Public Sub New()
        Me.CellTemplate = New DataGridViewFlagCell
        ...
    End Sub

End Class

In the constructor, I assign the CellTemplate to the second class: DataGridViewFlagCell, which inherits from System.Windows.Forms.DataGridViewImageCell. This class has some basic methods and functions: it retrieves the right icon and the right backcolor, and the Paint method will use them to paint the cells.

This class also provides a different backcolor when the mouse enters/leaves the cell. Have fun!