Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Anyway to display color for each item in DataGridViewComboBoxColumn

Status
Not open for further replies.

annarene

Programmer
Jul 7, 2008
17
US
Hello: I've been searching via the internet ...and don't think this is possible, but wanted to see if anyone here knows for sure.

I have a datagridview that currently has a color column
using DataGridViewComboBoxColumn drop-down...where as each item in the drop-down is a Color name:

e.g.
Red
Green
Blue

I would like to display the actual color instead of displaying the color name ... or maybe even just change the font of the color name so that it matches the color name.

does anyone know if there's a way to do this?

thank you for your help,
annarene
 
When you create the item for the drop down list you should be able to set the properties of the item. if you are adding a drop down to the datagrid then you should be able set the back color as you add the items to the combo box, before you add it (the combo box) to the drop down.


-Sometimes the answer to your question is the hack that works
 
Qik3Coder: Hello - thanks for your response to my post.
ok...I've decided for now, to attempt to do this w/ a combo box that's not in the datagridview and see if I can get that to work first.

Here's my code:

Code:
  'testing trying to add colors into the cboShowColors dropdown
        Dim myDB As New OleDb.OleDbConnection(sConnectionString)
        Dim myDA As New OleDb.OleDbDataAdapter("Select Color from tblColors order by Colorid", myDB)
        Dim myDS As New Data.DataSet
        Dim myDR As Data.DataRow


        myDA.Fill(myDS, "test")
        Dim test As DataTable
        test = myDS.Tables("test")
        Dim sColor As Drawing.Color
        If test.Rows.Count > 0 Then
            Dim i As Integer = 0
            For Each myDR In test.Rows
                With cboShowColors
                    Select Case myDR("Color")
                        Case "Red"
                            sColor = Color.Red
                        Case "White"
                            sColor = Color.White
                        Case "Cyan"
                            sColor = Color.Cyan
                        Case "Blue"
                            sColor = Color.Blue
                        Case "Green"
                            sColor = Color.Green
                        Case "Yellow"
                            sColor = Color.Yellow
                        Case "Magenta"
                            sColor = Color.Magenta
                        Case "Black"
                            sColor = Color.Black
                    End Select
                    .Items.Add(i)
                    .BackColor = sColor
                End With
            Next
        End If
        myDB.Close()

check out my attachement to see what's happening.
it's setting the backcolor for all the items based on the last item it gets. e.g. it got a value of "Yellow" so when I open the drop-down, all the items have a Yellow background.

How can I make it so each item is a different color?

Thanks again for your help,
annarene
 
two things:
This is a two step process, but you need to explicitly state it, to force the object re-creation.
Code:
Dim b As New Color 
b = ColorTranslator.FromHtml("red")

This needs to be inside your for loop, this will force it to "reset", because you are having a "wonderful" pointer issue. It is remembering the location in memory of the color object, so without a NEW, it just changes the data, rather than making a "freshie"


-Sometimes the answer to your question is the hack that works
 
By the way, your case statement is case sensitive, whereas the FromHTML is not.

-Sometimes the answer to your question is the hack that works
 
Qik3Coder:

makes sense what you're saying...

here's my code thus far:

Code:
Private Sub Showcolors()
        ''testing trying to add colors into the cboShowColors dropdown
        Dim myDB As New OleDb.OleDbConnection(sConnectionString)
        Dim myDA As New OleDb.OleDbDataAdapter("Select Color from tblColors order by Colorid", myDB)
        Dim myDS As New Data.DataSet
        Dim myDR As Data.DataRow



        myDA.Fill(myDS, "test")
        Dim test As DataTable
        test = myDS.Tables("test")
        If test.Rows.Count > 0 Then
            Dim i As Integer = 0
            For Each myDR In test.Rows

                With cboShowColors
                    Select Case myDR("Color")
                        Case "Red"
                            Dim b As New Color
                            b = ColorTranslator.FromHtml("red")
                            .Items.Add(myDR("Color"))
                            .BackColor = b

                        Case "White"
                            Dim b As New Color
                            b = ColorTranslator.FromHtml("white")
                            .Items.Add(myDR("Color"))
                            .BackColor = b

                        Case "Cyan"
                            Dim b As New Color
                            b = ColorTranslator.FromHtml("cyan")
                            .Items.Add(myDR("Color"))
                            .BackColor = b

                        Case "Blue"
                            Dim b As New Color
                            b = ColorTranslator.FromHtml("blue")
                        Case "Green"
                            Dim b As New Color
                            b = ColorTranslator.FromHtml("green")
                            .Items.Add(myDR("Color"))
                            .BackColor = b

                        Case "Yellow"
                            Dim b As New Color
                            b = ColorTranslator.FromHtml("yellow")
                            .Items.Add(myDR("Color"))
                            .BackColor = b

                        Case "Magenta"
                            Dim b As New Color
                            b = ColorTranslator.FromHtml("magenta")
                            .Items.Add(myDR("Color"))
                            .BackColor = b

                        Case "Black"
                            Dim b As New Color
                            b = ColorTranslator.FromHtml("black")
                            .Items.Add(myDR("Color"))
                            .BackColor = b

                    End Select

                End With
            Next
        End If
        myDB.Close()
    End Sub

It's still doing the same thing...making all items Yellow.

I don't think I'm doing these line correct:

.Items.Add(myDR("Color"))
.BackColor = b

does it look like I'm doing it correctly?

Thanks,
annarene
 
Close, i missed it before, you are setting the combo boxes back color, you want to set the Items back color,which may be possible, not sure.

I am going to play with it and see what i can come up with.


You totally missed the point of the color.fromHTML...

With this you do not need the select case. Look at your case statement and see that you do not need it at all.

-Sometimes the answer to your question is the hack that works
 
OK, so, i found an MSDN example:
and hacked it into this, which is close, I can't spend much more time on this at work...

Just add a fresh vb class to your project and replace it with this code. then build. You should be able to create one of these at runtime.

Code:
Public Class udcColorOnlyComboBox : Inherits ComboBox
    Private Colors() As String = New String() {"", "Red", "Green", "Blue", "Orange"}

    Public Sub New()
        InitializeComboBox()
    End Sub

    ' This method initializes the owner-drawn combo box.
    ' The drop-down width is set much wider than the size of the combo box
    ' to accomodate the large items in the list.  The drop-down style is set to 
    ' ComboBox.DropDown, which requires the user to click on the arrow to 
    ' see the list.
    Private Sub InitializeComboBox()
        Me.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable
        Me.Location = New System.Drawing.Point(10, 20)
        Me.Name = "ComboBox1"
        Me.Size = New System.Drawing.Size(100, 120)
        Me.DropDownWidth = 250
        Me.TabIndex = 0
        Me.DropDownStyle = ComboBoxStyle.DropDown
        Me.DataSource = Colors
    End Sub

    ' If you set the Draw property to DrawMode.OwnerDrawVariable, 
    ' you must handle the MeasureItem event. This event handler 
    ' will set the height and width of each item before it is drawn. 
    Private Sub ComboBox1_MeasureItem(ByVal sender As Object, _
       ByVal e As System.Windows.Forms.MeasureItemEventArgs) _
           Handles Me.MeasureItem
        e.ItemHeight = 15
        e.ItemWidth = Me.Width
    End Sub

    ' You must handle the DrawItem event for owner-drawn combo boxes.  
    ' This event handler changes the color, size and font of an 
    ' item based on its position in the array.
    Private Sub ComboBox1_DrawItem(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.DrawItemEventArgs) _
        Handles Me.DrawItem

        'Dim size As Single
        Dim myFont As System.Drawing.Font = New Font(FontFamily.GenericSerif, 8.25, FontStyle.Regular)
        Dim backColor As System.Drawing.Color = ColorTranslator.FromHtml(Colors(e.Index))
        ' Draw the background of the item.
        e.DrawBackground()
        ' Create a square filled with the animals color. Vary the size
        ' of the rectangle based on the length of the animals name.
        Dim rectangle As Rectangle = New Rectangle(2, e.Bounds.Top + 2, _
            e.Bounds.Height, e.Bounds.Height - 4)
        e.Graphics.FillRectangle(New SolidBrush(backColor), rectangle)
        ' Draw each string in the array, using a different size, color,
        ' and font for each item.
        e.Graphics.DrawString(Colors(e.Index), myFont, System.Drawing.Brushes.Black, _
            New RectangleF(e.Bounds.X + rectangle.Width, e.Bounds.Y, _
            e.Bounds.Width, e.Bounds.Height))

        ' Draw the focus rectangle if the mouse hovers over an item.
        e.DrawFocusRectangle()
    End Sub
End Class

-Sometimes the answer to your question is the hack that works
 
Hi Qik3Coder: I just now started looking at the code you sent...but wanted to say thank you. I'll give it a try and let you know how it turns out.

Thanks again for spending time on it.

Oh also, i now see your point about not needing the select case at all in my code above. Thanks for bringing that to my attention.

Thanks again.
annarene
 
Annarene, did you get this all straightened out?

-Sometimes the answer to your question is the hack that works
 
Hi Qik3Coder - thanks for checking in w/ me. Last week I got pulled off the project for a few days in order to deal w/ something else; however, I'm back on it today so will be trying to see if I can get what you sent me to work.

Thanks again for seeing how my project's going. I will be in touch shortly to let you know or maybe ask questions if need be.

AnnaRene
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top