Click here to Skip to main content
15,169,285 members
Articles / Multimedia / GDI+
Posted 24 Mar 2008


112 bookmarked

How to Host a Color Picker Combobox in Windows Forms DataGridView Cells

Rate me:
Please Sign up or sign in to vote.
4.78/5 (31 votes)
24 Mar 2008CPOL3 min read
An article on how to add a color picker ComboBox to DataGridView
Image 1


The .NET Framework provides quite a rich collection of UI controls and components for WinForms development. There is one particular control that has been missing. I am talking about a color-picker control with drop down color selection capabilities, just like the one used within the Visual Studio .NET property browser for editing Color-typed properties.

A color picker control was developed by Palo Mraz and was published in this Website. I wanted to use this control in a DataGridView but I couldn't find any example on how to do it. The only available examples on how to use custom control in DataGridView were to do with textual controls. This custom control involves graphics and not just text. I have decided to publish this article for the benefit of other fellow developers who would like to have a color picker combo box hosted by a DataGridView.


For those of you who want to get some background on the color picker combo box, please refer to "The ColorPicker WinForms Control".

You can get background on how to host custom controls in DataGridView cells here.

Color Picker in DataGridView

The core requirement for my project was to display the same drop down color selector that is used within WinForm’s PropertyGrid control inside a DataGridView.

The DataGridView control provides several properties that you can use to adjust the appearance and basic behavior (look and feel) of its cells, rows, and columns. My requirement was to show the color itself next to a text showing the color’s name.

In order to do that, I had to implement owner drawing for the control and extend its capabilities by creating custom cells, columns, and rows.

ColorPickerColumn Class

This class creates a custom column for hosting a column of color picker cells. It inherits from DataGridViewColumn and overrides the property CellTemplate.

public override DataGridViewCell CellTemplate
        return base.CellTemplate;
        // Ensure that the cell used for the template is a ColorPickerCell.
        if (value != null && 
            throw new InvalidCastException("Must be a ColorPicker");
        base.CellTemplate = value;

ColorPickerCell Class

This class creates a custom cell for hosting the color picker combo box and it inherits from DataGridViewTextBoxCell. In order to draw the content of the cell in the way I wanted, I overrode the paint method.

 protected override void Paint(Graphics graphics,
           Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
           DataGridViewElementStates elementState, object value,
           object formattedValue, string errorText,
           DataGridViewCellStyle cellStyle,
           DataGridViewAdvancedBorderStyle advancedBorderStyle,
           DataGridViewPaintParts paintParts)
    formattedValue = null;
    base.Paint(graphics, clipBounds, cellBounds, rowIndex, 
            elementState, value, formattedValue,
               errorText, cellStyle, advancedBorderStyle, paintParts);

    Rectangle ColorBoxRect = new Rectangle();
    RectangleF TextBoxRect = new RectangleF();
    GetDisplayLayout(cellBounds, ref ColorBoxRect, ref TextBoxRect);

    /// Draw the cell background, if specified.
    if ((paintParts & DataGridViewPaintParts.Background) ==
        SolidBrush cellBackground;
        if (value != null && value.GetType() == typeof(Color))
            cellBackground = new SolidBrush((Color)value);
            cellBackground =  new SolidBrush(cellStyle.BackColor);
        graphics.FillRectangle(cellBackground, ColorBoxRect);
        graphics.DrawRectangle(Pens.Black, ColorBoxRect);
        Color lclcolor=(Color)value;
        graphics.DrawString(lclcolor.Name.ToString(), cellStyle.Font, 
            System.Drawing.Brushes.Black, TextBoxRect);

The other method that I had to override is ParseFormattedValue. When the user picks a custom color from the list, he or she gets a Hex number of this color. There is a slight problem with this number: it doesn't get the 0x prefix added to it. This causes the System.Number.StringToNumber method to generate an exception. The .NET environment tries to convert this string to an integer and fails as without the 0x prefix it cannot be regarded as a Hex number, hence this method adds the prefix when required.

public override object ParseFormattedValue
    object formattedValue, DataGridViewCellStyle cellStyle, 
         System.ComponentModel.TypeConverter formattedValueTypeConverter, 
         System.ComponentModel.TypeConverter valueTypeConverter)
    int result;
    string number = "0x" + formattedValue.ToString();
    if (int.TryParse(formattedValue.ToString(), 
        stem.Globalization.NumberStyles.HexNumber, null, out result))
        //Hex number
        return base.ParseFormattedValue("0x" + formattedValue.ToString(), 
            lStyle, formattedValueTypeConverter, valueTypeConverter);
        return base.ParseFormattedValue(formattedValue, cellStyle, 
            mattedValueTypeConverter, valueTypeConverter);

ColorPickerControl Class

This class creates the custom control to be hosted by the ColorPickerCell. It implements the interface IDataGridViewEditingControl and overrides the OnLeave event of the original ColorPicker control. OnLeave – This event calls NotifyDataGridViewOfValueChange to notify the DataGridView that the contents of the cell have changed.

protected override void OnLeave(EventArgs eventargs)
    // Notify the DataGridView that the contents of the cell
    // have changed.

Using the Code

I created a form named ExampleForm containing a DataGridView control. This grid has two columns: the first is a simple textbox column and the second is the ColorPicker column. In this form, I demonstrate how to load and save the data containing colors and names to and from an XML file named ColorData.xml. The file should be stored in the same folder of the executable.


  • March 25th, 2008: Initial release

About the Author

I live in New Zealand. I've been doing Microsoft Windows development for the past 6 years.


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


About the Author

Software Developer
New Zealand New Zealand
No Biography provided

Comments and Discussions

QuestionVS 2019 - Hangs after color selector popup Pin
LightTempler8-Jan-21 8:24
MemberLightTempler8-Jan-21 8:24 
Questionwhere are the souce code of DataGridViewCustomColumn ? Pin
riccardo6823-Jun-14 1:36
Memberriccardo6823-Jun-14 1:36 
where are the souce code of referenced assembly named DataGridViewCustomColumn Confused | :confused:
thank you
AnswerRe: where are the souce code of DataGridViewCustomColumn ? Pin
riccardo6823-Jun-14 1:42
Memberriccardo6823-Jun-14 1:42 
Questiona bout the License Pin
H-M-Kais12-Mar-14 7:43
MemberH-M-Kais12-Mar-14 7:43 
GeneralMy vote of 5 Pin
Mazen el Senih29-Mar-13 6:35
professionalMazen el Senih29-Mar-13 6:35 
QuestionPlease help [modified] Pin
opium_2100210025-Feb-10 4:44
Memberopium_2100210025-Feb-10 4:44 
GeneralProject in VB Pin
Filippo Monti24-Jan-10 3:54
professionalFilippo Monti24-Jan-10 3:54 
GeneralDual monitor problem with popup of colorcombobox PinPopular
jcgsell11-Nov-09 15:57
Memberjcgsell11-Nov-09 15:57 
GeneralRe: Dual monitor problem with popup of colorcombobox Pin
srcarr30-Aug-12 12:16
Membersrcarr30-Aug-12 12:16 
GeneralProblem with the splitter leaving a trail in the editing control. [modified] Pin
Member 33644967-Sep-09 9:10
MemberMember 33644967-Sep-09 9:10 
Generalsub grid in grid Pin
drorby9-Jul-09 2:59
Memberdrorby9-Jul-09 2:59 
GeneralUnable to change color according to the options that i choose. Pin
lousyprogrammer25-Mar-09 23:20
Memberlousyprogrammer25-Mar-09 23:20 
GeneralRe: Unable to change color according to the options that i choose. Pin
Joe2003320-Apr-09 19:25
MemberJoe2003320-Apr-09 19:25 
GeneralA nice simple example Pin
Colin Eberhardt17-Nov-08 5:11
MemberColin Eberhardt17-Nov-08 5:11 
GeneralRe: A nice simple example Pin
Joe2003319-Nov-08 9:25
MemberJoe2003319-Nov-08 9:25 
GeneralMy Custom control remains clipped within the Datagridview Pin
TheCubanP29-Oct-08 4:26
MemberTheCubanP29-Oct-08 4:26 
GeneralActivating the Cell Pin
Ali Rafiee8-Jul-08 7:37
MemberAli Rafiee8-Jul-08 7:37 
GeneralRe: Activating the Cell Pin
Joe200339-Jul-08 14:29
MemberJoe200339-Jul-08 14:29 
GeneralRe: Activating the Cell Pin
Ali Rafiee10-Jul-08 6:22
MemberAli Rafiee10-Jul-08 6:22 
QuestionCan you make multiple cells remain in edit mode in the DataGridView? [modified] Pin
DeadHead720-Jun-08 5:09
MemberDeadHead720-Jun-08 5:09 
AnswerRe: Can you make multiple cells remain in edit mode in the DataGridView? Pin
Joe200339-Jul-08 14:34
MemberJoe200339-Jul-08 14:34 
QuestionVB wanted? Pin
dherrmann5-Apr-08 7:22
Memberdherrmann5-Apr-08 7:22 
AnswerRe: VB wanted? Pin
Joe200337-Apr-08 11:27
MemberJoe200337-Apr-08 11:27 
AnswerRe: VB wanted? Pin
Joe2003315-Apr-08 14:53
MemberJoe2003315-Apr-08 14:53 
GeneralValue Not Staying In Control After Leaving Cell Pin
vnmatt29-Mar-08 9:13
Membervnmatt29-Mar-08 9:13 

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.