Link to home
Start Free TrialLog in
Avatar of yuvaratna
yuvaratna

asked on

changing the back colour of the datagridview if the column contains something

i have a datagridview with 7 columns...and the 7th column is "approved" , if the cell approved is not empty, then i wanted to change the back colour of the particular row....how can i do this...

for now, i am using this code to alternate colours....

 With Me.DataGridView1
            .RowsDefaultCellStyle.BackColor = Color.Bisque
            .AlternatingRowsDefaultCellStyle.BackColor = Color.Beige
        End With

is there some property like

if datagridview1.columns(6).text is not nothing then
datagridview1.....backcolor = color.red
Avatar of jppinto
jppinto
Flag of Portugal image

Use something like the code below.

jppinto
Public Sub ColorMyGrid()
Dim myValue as Integer
        For Each row As DataGridViewRow In YourDataGridView.Rows
               myValue = row.Cells.Item("FieldColumn").value
 
               If myValue = 5 Then
                     row.DefaultCellStyle.BackColor = color.green
               ElseIF myOrderDate < Now() Then 
                     row.DefaultCellStyle.BackColor = color.blue
               End if
        Next
End Sub

Open in new window

Avatar of yuvaratna
yuvaratna

ASKER

using this code...

 For Each row As DataGridViewRow In Me.DataGridView1.Rows
            If Not row.IsNewRow Then
                Dim disapprovedcell As DataGridViewTextBoxCell = DirectCast(row.Cells(6), DataGridViewTextBoxCell)
                Dim disapproveddate As String = disapprovedcell.Value.ToString
                If Not disapproveddate Is "" Then
                    DataGridView1.RowsDefaultCellStyle.BackColor = Color.Red
                End If
            End If
        Next

in one line what i wanted to do is...if the disapproved date column is nbot null, then i wanted to change the backcolor of that particular row...in the above code everything is working fine..i.e execution is going in to if condition if the column is not null, but the property " DataGridView1.RowsDefaultCellStyle.BackColor = Color.Red"

is changing the backcolor of all the rows....i dont want this..i want to change the back color of the row in which the dispproved date is not null...

For Each row As DataGridViewRow In Me.DataGridView1.Rows
            If Not row.IsNewRow Then
                Dim disapprovedcell As DataGridViewTextBoxCell = DirectCast(row.Cells(6), DataGridViewTextBoxCell)
                Dim disapproveddate As String = disapprovedcell.Value.ToString
                If Not disapproveddate Is "" Then
                    DataGridView1.RowsDefaultCellStyle.BackColor = Color.Red
                End If
            End If
        Next

Open in new window

For Each row As DataGridViewRow In Me.DataGridView1.Rows
            If Not row.IsNewRow Then
                Dim disapprovedcell As DataGridViewTextBoxCell = DirectCast(row.Cells(6), DataGridViewTextBoxCell)
                Dim disapproveddate As String = disapprovedcell.Value.ToString
                If disapproveddate Is "" Then
                    DataGridView1.RowsDefaultCellStyle.BackColor = Color.Red
                End If
            End If
        Next
Remove the Not in your If...
thanlks ippinto....thats not  what i want...the if condition is working in the way i wanted...but i dont want to change the backcolor of all the cells...i wanted to change the back color of only one row....is there any property for that????

the line..."DataGridView1.RowsDefaultCellStyle.BackColor = Color.Red"    is changing the back color of all the rows...
for example:

Me.Datagridview1.CurrentRow.Cells(7).Style.BackColor() = Color.Red
nope...that line is geting executed, but the back color is not getting changed...
Did you've changed the code like this:

Me.Datagridview1.CurrentRow.Cells(6).Style.BackColor() = Color.Red
yup!, i changed the code to

Me.Datagridview1.CurrentRow.Cells(6).Style.BackColor() = Color.Red

but that didnt change the color....couldnt figure why...
ASKER CERTIFIED SOLUTION
Avatar of jppinto
jppinto
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial