PC Review


Reply
Thread Tools Rate Thread

Changing Row Color in DataGridView

 
 
Terry Olsen
Guest
Posts: n/a
 
      18th Feb 2007
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


 
Reply With Quote
 
 
 
 
lord.zoltar@gmail.com
Guest
Posts: n/a
 
      18th Feb 2007
On Feb 18, 1:15 pm, "Terry Olsen" <tolse...@hotmail.com> wrote:
> 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.

 
Reply With Quote
 
ClayB
Guest
Posts: n/a
 
      18th Feb 2007
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.





 
Reply With Quote
 
ShaneO
Guest
Posts: n/a
 
      19th Feb 2007
Terry Olsen wrote:
> 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.
 
Reply With Quote
 
ShaneO
Guest
Posts: n/a
 
      19th Feb 2007
ShaneO wrote:
>
> 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.
 
Reply With Quote
 
Terry Olsen
Guest
Posts: n/a
 
      19th Feb 2007
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.

"ShaneO" <(E-Mail Removed)> wrote in message
news:45d8e9bd$0$23346$(E-Mail Removed)...
> Terry Olsen wrote:
>> 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.



 
Reply With Quote
 
ShaneO
Guest
Posts: n/a
 
      19th Feb 2007
Terry Olsen wrote:
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
no color in datagridview in mdi form Kayıhan Microsoft C# .NET 1 2nd Sep 2009 10:29 AM
Changing color of a DataGrid/DataGridView row RP Microsoft C# .NET 1 3rd Dec 2007 10:26 AM
DataGridView Changing Cell Color =?Utf-8?B?UGV0ZXI=?= Microsoft Dot NET Framework Forms 1 13th Sep 2007 06:34 AM
color one cell in DataGridView Jassim Rahma Microsoft C# .NET 1 12th Aug 2007 11:59 PM
DataGridView row background color =?Utf-8?B?bWFydGluMQ==?= Microsoft VB .NET 6 28th Jun 2006 05:33 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:16 AM.