[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

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
0
yuvaratna
Asked:
yuvaratna
  • 6
  • 5
1 Solution
 
jppintoCommented:
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

0
 
yuvaratnaAuthor Commented:
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...
0
 
yuvaratnaAuthor Commented:

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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jppintoCommented:
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
0
 
jppintoCommented:
Remove the Not in your If...
0
 
yuvaratnaAuthor Commented:
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...
0
 
jppintoCommented:
for example:

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

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

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

but that didnt change the color....couldnt figure why...
0
 
jppintoCommented:
Try the code below please.

jppinto
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
      If Not String.IsNullOrEmpty(disapproveddate) Then
         row.DefaultCellStyle.BackColor = Color.Red
      Else
         row.DefaultCellStyle.BackColor = Color.White
      End If
   Else
      row.DefaultCellStyle.BackColor = Color.White
   End If
Next

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now