Datagridview.forecolor won't change!

mpooley

Active member
Joined
Jan 4, 2009
Messages
32
Programming Experience
Beginner
Hi all
I have a fairly basic problem that I just can't solve.
I am trying to change the font color in a collection of grids everything else like background color font etc works ok
but I cant get the forecolor to change.

I have tried DataGridView1.ForeColor = blahblah.color and DataGridView1.DefaultCellStyle.ForeColor = blahblah.color

I am assuming it's an easy answer but I have googled many variations and not found an answer
can anyone help me please

Mike
 
I just did a test for myself with this code:
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim table As New DataTable

        With table.Columns
            .Add("ID", GetType(Integer))
            .Add("Name", GetType(String))
        End With

        With table.Rows
            .Add(1, "Peter")
            .Add(2, "Paul")
            .Add(3, "Mary")
        End With

        Me.DataGridView1.DataSource = table
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.DataGridView1.ForeColor = Color.Red
    End Sub

End Class
I ran the project and the grid populated with black text. I clicked the button and all the text turned red.
 
Thanks That sort of proves that something else must be happening here. I'm almost certain that I had this working before too !
just can't figure out what might be stopping it working.
I tested the code is providing the right colors to the datagrid by stepping through it.

I'll just keep plugging at it .

Thanks
 
In situations like this, I tend to create a new project that isolates just the feature that I'm having an issue with, e.g. what I posted earlier, and then build it up bit by bit to resemble my original project to see at what point the feature breaks. That usually provides a good indication of where the issue is.
 
Yes I agree that's a good strategy, the original project is quite complex and large with the grids on home made user controls etc so was hoping I wouldn't have to go down that route lol
 
Just thought I would leave my method of solving this problem in case it happens to anyone else.

I added the code to change forecolor in another grid on a different form but used the same methods as the one I was having trouble with. This worked perfectly.
I treble checked my code wasn't altering the color more than once! in different places. it was not. I Checked that the new grids properties were exactly the same in every way as the dodgy one. They were !
So It seemed like it must be the grid itself or underlying code. So I dropped a new grid on top of the old grid Changed the names and expected it would work.
It didn't !! fontcolor still would not change
So I deleted the new grid and the code magically changed itself (in 32 places) to ''dataviewgrid2'' (the temporary name I had given my original grid)
which I didn't think it would do?
ran the code expecting it would still not work BUT it DID !!! :glee:
 
Last edited:
Damn I have to admit it was my fault all along :blue:
what had happened was that when I did all the above, the cellFormatting event handler was disconnected from the grid. When I realised and put the handler back in.
it stopped working again and it was very obvious why! I had code that changed the color of the text in row when certain criteria was met. I had hard coded the forecolor in there when I was testing and forgotten to go back and do it properly.
I looked at that code quite a few times but missed it every time doh!

it just proves it's ALWAYS my fault lol

Thanks
 
Back
Top