I have One DatagridView.
I need to highlights rows which belongs to "ID"
For Example
ID
1
1
1
1
2
2
2
3
That's it.
I need to highlights ID-1 all rows to Yellow Color
Then 2 to Lime Color.
And 3 to Yellow Again.

Here is my Code But Not working. Any Help will be really appreciated.
For i As Integer = 0 To dgvvoucher.Rows.Count - 1
                For j As Integer = 0 To dgvvoucher.Rows.Count - 1
                    If dgvvoucher.Item(0, i).Value = dgvvoucher.Item(0, i).Value Then
                        dgvvoucher.Rows(i).DefaultCellStyle.BackColor = Color.Yellow
                    Else
                        dgvvoucher.Rows(i).DefaultCellStyle.BackColor = Color.Lime
                    End If
                Next
            Next

Recommended Answers

All 9 Replies

could you write me some codes Examples?

Did the link did not provide one?
Perhaps this is all you need.

Thank you for replying sir ..
but it's no for my question ..

My dgv is 5 column show in attach
i want to only highlight the row by ID value.

Data is Preloading As Shown in Picture .
When i click the button ...

All rows of Number 1 are yellow.
All rows of Number 2 are lime.
And
All rows of Number 5 are yellow again.

How to code ?

I think it may using like For method with Step - 1 ?
But i'm not coded.

Sorry for my english . Thank you.

All i Need is that result When i Click the Button.
I do it by photoshop :D
ID Number are increasing and duplicating later. I want to highlights all result
like these attachment photo.

Perhaps try something like this:

// walk through rows of dgv with for-loop
            if (Convert.ToInt32(row.Cells[0].Value) == 1) //ID of first column equals 1
            {
                row.DefaultCellStyle.BackColor = Color.Red;
            }
            else if(Convert.ToInt32(row.Cells[0].Value) == 1)
            { 
                row.DefaultCellStyle.BackColor = Color.Green; 
            }

Please change Cells[0].Value to Cells(0).Value
Perhaps a for each ... next loop is more appropriate also.

' walk through rows of dgv with for-loop
If Convert.ToInt32(row.Cells(0).Value) = 1 Then
    'ID of first column equals 1
    row.DefaultCellStyle.BackColor = Color.Red
ElseIf Convert.ToInt32(row.Cells(0).Value) = 1 Then
    row.DefaultCellStyle.BackColor = Color.Green
End If

I write with VB.NET but I don't know what is mean 'row'
it cause error.
Thanks again.

Code is working . Here is my code .. but all rows are hightlights red color.
whatever thank you for answering.

 ' walk through rows of dgv with for-loop
                If Convert.ToInt32(dgvvoucher.Rows(i).Cells(0).Value) = 1 Then
                    'ID of first column equals 1
                    dgvvoucher.DefaultCellStyle.BackColor = Color.Red
                ElseIf Convert.ToInt32(dgvvoucher.Rows(i).Cells(0).Value) > 1 Then
                    dgvvoucher.DefaultCellStyle.BackColor = Color.Green
                End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.