Changing Row Color in DataGridView

T

Terry Olsen

I have two DataGridView's loaded from their respective datasources. I want
to highlight rows in one DataGridView that are also in the other
DataGridView. But the following code does not work. Looking for some
suggestions:

Private Sub ShowPlaylistSongsInLibraryGrid()

For i As Integer = 0 To gridLibrary.Rows.Count - 1

For j As Integer = 0 To gridPlaylist.Rows.Count - 1

If gridLibrary.Rows(i).Cells("FilePath").Value =
gridPlaylist.Rows(j).Cells("FilePath").Value Then

gridLibrary.Rows(i).DefaultCellStyle.BackColor = Color.Yellow

End If

Next

Next

End Sub
 
L

lord.zoltar

I have two DataGridView's loaded from their respective datasources. I want
to highlight rows in one DataGridView that are also in the other
DataGridView. But the following code does not work. Looking for some
suggestions:

Private Sub ShowPlaylistSongsInLibraryGrid()

For i As Integer = 0 To gridLibrary.Rows.Count - 1

For j As Integer = 0 To gridPlaylist.Rows.Count - 1

If gridLibrary.Rows(i).Cells("FilePath").Value =
gridPlaylist.Rows(j).Cells("FilePath").Value Then

gridLibrary.Rows(i).DefaultCellStyle.BackColor = Color.Yellow

End If

Next

Next

End Sub

I think if you want to change the colour of a datagridview row or
cell, you have to do it after all the data is loaded and displayed. I
usually do it from the cell formatting event and it works for my
needs, but there might be other/better ways to do it.
 
C

ClayB

If you use

gridLibrary.Rows(i).DefaultCellStyle.BackColor = Color.Yellow

to set the color, then you will need to reset the colors after the
grid is sorted.
===================
Clay Burch
Syncfusion, Inc.
 
S

ShaneO

Terry said:
I have two DataGridView's loaded from their respective datasources. I want
to highlight rows in one DataGridView that are also in the other
DataGridView. But the following code does not work. Looking for some
suggestions:
I copied your code into a test project and it worked perfectly.

I also simplified your code with the following -

For i As Integer = 0 To gridLibrary.RowCount - 1
For j As Integer = 0 To gridPlaylist.RowCount - 1
If gridLibrary.Item(0, i).Value = gridPlaylist.Item(0, j).Value Then
gridLibrary.Rows(i).DefaultCellStyle.BackColor = Color.Yellow
End If
Next
Next

This also worked fine.

Maybe you aren't actually calling your "ShowPlaylistSongsInLibraryGrid"
sub at all?

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
S

ShaneO

ShaneO said:
If gridLibrary.Item(0, i).Value = gridPlaylist.Item(0, j).Value Then
Sorry, for your purposes the above should have read -

If gridLibrary.Item("FilePath", i).Value = gridPlaylist.Item("FilePath",
j).Value Then

I used 0 in my test.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
T

Terry Olsen

No, here's the code from my Form_Load....

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
gridLibrary.DataSource = LoadLibraryGrid()
gridLibrary.Columns("FilePath").Visible = False
gridLibrary.Show()
gridPlaylist.DataSource = LoadPlaylistGrid()
gridPlaylist.Columns("FilePath").Visible = False
gridPlaylist.Show()
gridAddMusicToLibraryInitialize()
ShowPlaylistSongsInLibraryGrid()
End Sub

The grids get loaded properly, but my LibraryGrid isn't being highlighted.
 
S

ShaneO

Terry said:
No, here's the code from my Form_Load....
I wonder what would happen if you inserted a DoEvents before your
ShowPlaylistSongsInLibraryGrid call?

The other thing to try is to place a button on your Form and from its
Click Event call that same Sub. This way you will know that everything
is loaded and all Paint events etc have completed. As I wrote earlier,
I copied your exact code (without assigning the "FilePath" name to a
column) and it worked perfectly.

You're obviously going to need to step through this one to see what's
happening so maybe place a Break within your Sub to see what it's
actually doing.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 

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