We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,401 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

change row colour in datagridview based on some condition

I have datagridview which is populated with 3 columns partno,locality,houseno. Initially my row colour would be say light blue. Now what i want is whenever houseno changes in row,its row colour should be changed to say grey. That means whenever houseno changes,these two colours should be alternately applied to rows of grid.
For example,values that would be populated in houseno column will be
0
0
1
1
2
etc.Now for row with houseno 0 will have row colour as light blue ,for houseno 1 will have row colour grey,for houseno 2 again light blue and so on.
Can any1 help me with this.
Thanks in advance.

3
Contributors
3
Replies
3 Hours
Discussion Span
4 Months Ago
Last Updated
4
Views
Question
Answered
Rachna0309
Light Poster
36 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Try something like:

    Dim last_value As String = ""
    Dim last_color As Color = Color.LightBlue

    If DataGridView1.Rows.Count > 0 Then
        For Each row In DataGridView1.Rows

            If DataGridView1.Item("houseno", row.index).Value <> last_value Then
                If last_color = Color.LightBlue Then
                    DataGridView1.Rows(row.index).DefaultCellStyle.BackColor = Color.LightGray
                Else
                    DataGridView1.Rows(row.index).DefaultCellStyle.BackColor = Color.LightBlue
                End If
            Else
                DataGridView1.Rows(row.index).DefaultCellStyle.BackColor = last_color
            End If

            If DataGridView1.Rows(row.index).IsNewRow = False Then
                last_value = DataGridView1.Item("houseno", row.index).Value.ToString
                last_color = DataGridView1.Rows(row.index).DefaultCellStyle.BackColor
            End If

        Next

    End If
adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11

Make all cells in row 1 blue.

For Each c As DataGridViewCell In DataGridView1.Rows(1).Cells
        c.Style.BackColor = Color.Blue
Next
Reverend Jim
Carpe per diem
Moderator
3,612 posts since Aug 2010
Reputation Points: 563
Solved Threads: 451
Skill Endorsements: 32

Thanks..problem is solved.

Rachna0309
Light Poster
36 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 4 Months Ago by Reverend Jim and adam_k

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0650 seconds using 2.68MB