how to change a cell back or fore color in datagridview

S

snow

Hi All,

I want to change a cell backcolor and forecolor in a datagridview.
Here is my code:

DataGridView1.DataSource = dataTable1
DataGridView1.Item(1,1).Style.ForeColor = Color.Red
DataGridView1.Item(1,1).Style.BackColor = Color.Blue

When I run the program, the cell doesn't change. Why it doesn't work?

Thanks for the help!
 
O

Onur Güzel

Hi All,

I want to change a cell backcolor and forecolor in a datagridview.
Here is my code:

DataGridView1.DataSource = dataTable1
DataGridView1.Item(1,1).Style.ForeColor = Color.Red
DataGridView1.Item(1,1).Style.BackColor = Color.Blue

When I run the program, the cell doesn't change. Why it doesn't work?

Thanks for the help!

Hi,
That should help:
' That changes backcolor of Row(1) and Cell(1) to blue
DataGridView1.Rows(1).Cells(1).Style.BackColor = Color.Blue

' That changes forecolor(textcolor of cell) of the same cell to br
DataGridView1.Rows(1).Cells(1).Style.ForeColor = Color.Red

Plus, the code you posted also works OK.

Hope this helps,

Onur Güzel
 
S

snow

Hi,
That should help:
' That changes backcolor of Row(1) and Cell(1) to blue
DataGridView1.Rows(1).Cells(1).Style.BackColor = Color.Blue

' That changes forecolor(textcolor of cell) of the same cell to br
DataGridView1.Rows(1).Cells(1).Style.ForeColor = Color.Red

Plus, the code you posted also works OK.

Hope this helps,

Onur Güzel

I am sorry, none of them works for me. The program goes through these
code without any error, but the colors don't display. I am using
Visual Studio 2005 on VISTA Ultimate computer. Should I need to do
some special settings for the DataGridView control?

Thanks!
 
S

snow

I am sorry, none of them works for me. The program goes through these
code without any error, but the colors don't display. I am using
Visual Studio 2005 on VISTA Ultimate computer. Should I need to do
some special settings for the DataGridView control?

Thanks!- Hide quoted text -

- Show quoted text -

By the way, I can set colors for header columns at design time when I
set EnableHeadersVisualStyles = False. But at run time, It seems no
way for to set colors for a individual cell. What is the trick?
 
T

Teemu

By the way, I can set colors for header columns at design time when I
set EnableHeadersVisualStyles = False. But at run time, It seems no
way for to set colors for a individual cell. What is the trick?

Have you tried to put your code in DataGridView's Paint event?

-Teemu
 
O

Onur Güzel

I am sorry, none of them works for me. The program goes through these
code without any error, but the colors don't display. I am using
Visual Studio 2005 on VISTA Ultimate computer. Should I need to do
some special settings for the DataGridView control?

Thanks!- Hide quoted text -

- Show quoted text -

Sorry, actually i tested it on XP with VB 2005 Express by adding a new
datagridview control and some columns and rows by binding a datatable
as same as what you did. Maybe you're executing code in the wrong
event. So, also try to fill datagridview's datasource then try to
change cell style in a button_click or orher event sub with the code
above.

Plus, you can also try to create a new project with a fresh
Datagridview control.

HTH,

Onur Güzel
 
L

Lloyd Sheen

Hi,
That should help:
' That changes backcolor of Row(1) and Cell(1) to blue
DataGridView1.Rows(1).Cells(1).Style.BackColor = Color.Blue

' That changes forecolor(textcolor of cell) of the same cell to br
DataGridView1.Rows(1).Cells(1).Style.ForeColor = Color.Red

Plus, the code you posted also works OK.

Hope this helps,

Onur Güzel

I am sorry, none of them works for me. The program goes through these
code without any error, but the colors don't display. I am using
Visual Studio 2005 on VISTA Ultimate computer. Should I need to do
some special settings for the DataGridView control?

Thanks!

I just went thru this myself.

You can change the color of a row but not the color of a cell unless you are
in the CellFormatting event of the grid. I changed my code to the
CellFormatting and it worked no problem.

The one change is that you have to do the test for the color in the event
rather than when you might think would be the normal place within your code.

Example:

Private Sub CDListView_CellFormatting(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles
CDListView.CellFormatting
Dim dr As DataGridViewRow = CDListView.Rows(e.RowIndex)
Dim tmp As TmpArtistsCD = CType(dr.DataBoundItem, TmpArtistsCD)
Dim found As Boolean = False
For Each cd As ArtistsCD In q1
If tmp.Album.ToUpper = cd.Album.ToUpper And tmp.Artist = cd.Artist Then
found = True
Exit For
End If
Next
If Not found Then
dr.DefaultCellStyle.BackColor = Color.Tomato
End If

End Sub

Hope this helps
LS
 
S

snow

I am sorry, none of them works for me. The program goes through these
code without any error, but the colors don't display. I am using
Visual Studio 2005 on VISTA Ultimate computer. Should I need to do
some special settings for the DataGridView control?

Thanks!

I just went thru this myself.

You can change the color of a row but not the color of a cell unless you are
in the CellFormatting event of the grid.  I changed my code to the
CellFormatting and it worked no problem.

The one change is that you have to do the test for the color in the event
rather than when you might think would be the normal place within your code.

Example:

 Private Sub CDListView_CellFormatting(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles
CDListView.CellFormatting
  Dim dr As DataGridViewRow = CDListView.Rows(e.RowIndex)
  Dim tmp As TmpArtistsCD = CType(dr.DataBoundItem, TmpArtistsCD)
  Dim found As Boolean = False
  For Each cd As ArtistsCD In q1
   If tmp.Album.ToUpper = cd.Album.ToUpper And tmp.Artist = cd.Artist Then
    found = True
    Exit For
   End If
  Next
  If Not found Then
   dr.DefaultCellStyle.BackColor = Color.Tomato
  End If

 End Sub

Hope this helps
LS- Hide quoted text -

- Show quoted text -

I tried the code in CellFormatting and CellPainting events, both
work!
Thanks for the help!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top