VB.NET - coloring columns in a datagridview - Asked By jenn on 25-May-10 06:43 PM

afternoon all :)

I have a datagridview on a windows form, I want to choose a background color for each column. The colors arent based on any value..just want to change the back colors to kinda give it a dressy look. I dont want all the columns to be the same color. Can find alot of posts to change individual cell colors, or even cells based on content..but none of them fit what I want to do. I want to designate a certain color based on the HEADINGS of my columns. For example if my COLUMN heading is "Employee"..I would like that WHOLE column to show a color. And another column called "Status"..I want to assign a different back color to that WHOLE column. so if anyone can help me it would be appreciated

ThanxX :)
Phivos Stylianides replied to jenn on 25-May-10 08:02 PM
Hi Jenn,

You can create a style object for each column and assign it to the default cell style of the column. Here is an example:

Dim style As DataGridViewCellStyle = New DataGridViewCellStyle()
style.BackColor = Color.Yellow
style.ForeColor = Color.Black
style.SelectionBackColor = Color.Red
style.SelectionForeColor = Color.White
DataGridView1.Columns(0).DefaultCellStyle = style
Anoop S replied to jenn on 25-May-10 11:56 PM
you can try something like this

For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
   If Me.DataGridView1.Rows(i).Cells("Status").Value >= 90 Then
   Me.DataGridView1.Rows(i).Cells("Cell_to_Change").Style.BackColor = Color.Red
End If
Next
jenn replied to Anoop S on 26-May-10 10:50 AM
good morning :)

Okay, I want to ask why am I doing a Value=90?? I assume you can change the 90 to any value you want..but I am wondering what this is for??
would it be like in case I want the color to change in the status column based off a certain bay number???

for example: If I decided to have the ROWS / or Columns to change color based off a Bay Number (1,2,3 or 4) I would replace that 90 with Bay 1, or 2, or 3..ect??

something like this:
For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
   If Me.DataGridView1.Rows(i).Cells("Status").Value >= Bay 1 Then
   Me.DataGridView1.Rows(i).Cells("Cell_to_Change").Style.BackColor = Color.Red
End If
Next
For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
   If Me.DataGridView1.Rows(i).Cells("Status").Value >= Bay 2 Then
   Me.DataGridView1.Rows(i).Cells("Cell_to_Change").Style.BackColor = Color.Blue
End If
Next
For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
   If Me.DataGridView1.Rows(i).Cells("Status").Value >= Bay 3 Then
   Me.DataGridView1.Rows(i).Cells("Cell_to_Change").Style.BackColor = Color.Green
End If
jenn replied to Phivos Stylianides on 26-May-10 10:56 AM
Dim style As DataGridViewCellStyle = New DataGridViewCellStyle()
style.BackColor = Color.Yellow
style.ForeColor = Color.Black
style.SelectionBackColor = Color.Red
style.SelectionForeColor = Color.White
DataGridView1.Columns(Employee).DefaultCellStyle = style
  
Dim style As DataGridViewCellStyle = New DataGridViewCellStyle()
style.BackColor = Color.Yellow
style.ForeColor = Color.Black
style.SelectionBackColor = Color.Red
style.SelectionForeColor = Color.White
DataGridView1.Columns(TimeIn).DefaultCellStyle = style
good morning Phivos :)

okay I can see where your going with this..it looks almost identical to how I changes the header back and font colors.
Except now I am working with columns. Now is the Column(0) is that a standard start number or do I put the Name of the column in instead like this:
Also would I do this under each combobox that holds these fields or would I do this under the form_load event..

LOL...once again : where do i stick this!! LOL
Phivos Stylianides replied to jenn on 26-May-10 11:09 AM
Your code looks correct, creating a DataGridViewCellStyle object with the desired colors for each column. 

You can retrieve a column either by its index number (starts from 0)
eg: DataGridView1.Columns(0).DefaultCellStyle = style

or by the name of the column
eg: DataGridView1.Columns("Employee").DefaultCellStyle = style

This code has to be executed once so you can place it in Form_Load or maybe in a method called from the Form_Load in case you have additional logic there and don't what to make it very long.
eg:
private sub Form1_Load(ByVal sender as object, ByVal e as EventArgs)
InitializeGrid()
End Sub

private sub InitializeGrid()
' Style logic here...
End Sub
jenn replied to Phivos Stylianides on 26-May-10 11:23 AM
ahhh, okay

no I just need it to be kept simple..not based off of anything really, just stylish :)

but then I was lookin at Anoops post and my little brain started to wander LOL

what IF I did base the ROW Color off of the Bay selection made in the comboboxes..that would be kinda cool!

Just a thought..

guess the method I go with will just be based off of which way I can get to work at this time.

the only SET selections in the comboboxes are in the "Bay Number" box. so I figured it cant be that hard to actually color the rows according to the bay number selected..LOL, I mean there are only 5 bays..so 5 different colors! :)

I dunno, could be biting off more than I can chew hehehee, but figured might take a look at it anyways :)
jenn replied to Phivos Stylianides on 26-May-10 12:05 PM
Okay what am I doing wrong here? this is what I did:

and in debug it doesnt change anything :(

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim style As DataGridViewCellStyle = New DataGridViewCellStyle()
        style.BackColor = Color.Cornsilk
        style.ForeColor = Color.Black
        style.SelectionBackColor = Color.SlateGray
        style.SelectionForeColor = Color.White
        DataGridView1.Columns("Employee").DefaultCellStyle = style
        InitializeGrid()
    End Sub
    Private Sub InitializeGrid()
          
  
    End Sub
jenn replied to jenn on 26-May-10 12:12 PM
Okay, I tried this based off of Bay Number choices and it also didnt change anything:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
            If Me.DataGridView1.Rows(i).Cells("Status").Value >= "Bay1" Then
                Me.DataGridView1.Rows(i).Cells("Cell_to_Change").Style.BackColor = Color.Red
            End If
        Next
        For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
            If Me.DataGridView1.Rows(i).Cells("Status").Value >= "Bay2" Then
                Me.DataGridView1.Rows(i).Cells("Cell_to_Change").Style.BackColor = Color.Blue
            End If
        Next
        For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
            If Me.DataGridView1.Rows(i).Cells("Status").Value >= "Bay3" Then
                Me.DataGridView1.Rows(i).Cells("Cell_to_Change").Style.BackColor = Color.Green
            End If
        Next
  
  
    End Sub
jenn replied to jenn on 26-May-10 12:18 PM
OOoooO!!! Wait a sec..
I think I know what is going on here:

my gridview is set up to display choices made in comboboxes..
so do I need to go and set choices in the combo boxes?? and then they will change the grid colors based off the combo box?? I dunno about that cuz to me..it would seem that once the comboboxes are all selected and the choices hit the display..the display SHOULD pick up the choices and adjust the color accordingly LOL..or am I just giving the gridview too much credit..

IF I have to apply the changes in color to the combo boxes instead of the datagridview..how do I do that??

The comboboxes I have do not have predefined choices, the end user is adding choices to the combos and then sending to the display...which in turn is getting saved to a CSV File

the ONLY combo box that has predefined choices is the Bay Number box ( they hold options of Bay1-Bay5)
Phivos Stylianides replied to jenn on 26-May-10 12:21 PM
Your code is correct. However, keep in mind that a grid column has two naming properties: HeaderText and Name. HeaderText is what is displayed at the top of the column. Name is what you use inside your code to refer to that column so make sure that the Employee column for example has both those properties set to "Employee".

By the way you do not need the InitializeGrid method as you don't actually use it! What I meant earlier was, in case you have many other statements also in your page load -> put the grid styling logic in its own method and call that method from your form_load.

jenn replied to Phivos Stylianides on 26-May-10 02:59 PM
AHAA!!

LOL..you hit nail right on the head! :)

I did a peek and your absolutely correct, that is EXACTLY what I did!

In my collections box for the grid it is:
Header Text: Employee
Design Column Name: Column2

So, what I need to do is Change the Design Column Name to Employee As well :)

sheesh Phivos, you sure are good at this stuff! :)

Thank you so very much!
Phivos Stylianides replied to jenn on 26-May-10 03:00 PM
You are very welcome!!