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.Back Color = Color.Bisque
.AlternatingRowsDefaultCel lStyle.Bac kColor = Color.Beige
End With
is there some property like
if datagridview1.columns(6).t ext is not nothing then
datagridview1.....backcolo r = color.red
for now, i am using this code to alternate colours....
With Me.DataGridView1
.RowsDefaultCellStyle.Back
.AlternatingRowsDefaultCel
End With
is there some property like
if datagridview1.columns(6).t
datagridview1.....backcolo
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.ToSt ring
If Not disapproveddate Is "" Then
DataGridView1.RowsDefaultC ellStyle.B ackColor = 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.RowsDefaultC ellStyle.B ackColor = 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.ToSt
If Not disapproveddate Is "" Then
DataGridView1.RowsDefaultC
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.RowsDefaultC
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...
ASKER
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
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.ToSt ring
If disapproveddate Is "" Then
DataGridView1.RowsDefaultC ellStyle.B ackColor = Color.Red
End If
End If
Next
If Not row.IsNewRow Then
Dim disapprovedcell As DataGridViewTextBoxCell = DirectCast(row.Cells(6), DataGridViewTextBoxCell)
Dim disapproveddate As String = disapprovedcell.Value.ToSt
If disapproveddate Is "" Then
DataGridView1.RowsDefaultC
End If
End If
Next
Remove the Not in your If...
ASKER
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.Rows DefaultCel lStyle.Bac kColor = Color.Red" is changing the back color of all the rows...
the line..."DataGridView1.Rows
for example:
Me.Datagridview1.CurrentRo w.Cells(7) .Style.Bac kColor() = Color.Red
Me.Datagridview1.CurrentRo
ASKER
nope...that line is geting executed, but the back color is not getting changed...
Did you've changed the code like this:
Me.Datagridview1.CurrentRo w.Cells(6) .Style.Bac kColor() = Color.Red
Me.Datagridview1.CurrentRo
ASKER
yup!, i changed the code to
Me.Datagridview1.CurrentRo w.Cells(6) .Style.Bac kColor() = Color.Red
but that didnt change the color....couldnt figure why...
Me.Datagridview1.CurrentRo
but that didnt change the color....couldnt figure why...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
jppinto
Open in new window