IvanHowarth
asked on
vb.net (ver1) App Form: Is it possible to change an individual datagrid row forecolor based on a condition?
Is it possible to change an individual datagrid row forecolor based on a condition?
E.g. If one of the columns for that row was a negative number, then change the forecolor for that row to red, whilst all the others are black (assuming that their respective column is a positive number)?
E.g. If one of the columns for that row was a negative number, then change the forecolor for that row to red, whilst all the others are black (assuming that their respective column is a positive number)?
I tried looking for something like that as well, and from what I could find out, it is not possible. The only thing that you can do is just a column forecolor, but you are changing the forecolor for every cell in the column. The row object does not have that kind of property to change the forecolor. The only thing that I could find was just to highlight the row but not change the forecolor. That only works to an extent because when you need to highlight another row, you lose the highlight on the previous row of couse. I ended up using another approach to change the forecolor of a item in a datagrid format by using a rtf box.
ASKER
Upon some research myself, I tend to agree with you.
Is this achievable in VB.net ver2 do you know?
I'll leave this question open. If I don't get a response, I'll award you the points.
Is this achievable in VB.net ver2 do you know?
I'll leave this question open. If I don't get a response, I'll award you the points.
Use ItemDataBound event... something like this:
Private Sub MyDataGrid_ItemDataBound(B yVal sender As Object, ByVal e As System.Web.UI.WebControls. DataGridIt emEventArg s) Handles DataGrid1.ItemDataBound
' First make sure you're not in the header, footer, pager, etc
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingIt em) Then
If CInt(e.Item.Cells(2).Text) < 0 Then
e.Item.Cells(2).ForeColor = System.Drawing.Color.Red
End If
End If
End Sub
Private Sub MyDataGrid_ItemDataBound(B
' First make sure you're not in the header, footer, pager, etc
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingIt
If CInt(e.Item.Cells(2).Text)
e.Item.Cells(2).ForeColor = System.Drawing.Color.Red
End If
End If
End Sub
Oh if you wanted to change the entire row forecolor to red, use
e.Item.ForeColor = System.Drawing.Color.Red
instead of
e.Item.Cells(2).ForeColor = System.Drawing.Color.Red
e.Item.ForeColor = System.Drawing.Color.Red
instead of
e.Item.Cells(2).ForeColor = System.Drawing.Color.Red
nevermind :(
You're using windows forms... not web forms
You're using windows forms... not web forms
I tried using that myself but I could not find the event for a datagrid.
For version 2, I don't believe it is possible because that is the version I am using now.
For version 2, I don't believe it is possible because that is the version I am using now.
I think I found something a little more simpler, what you can do is this:
Private Sub datagrid_CellFormatting(By Val sender As Object, ByVal e As _ System.Windows.Forms.DataG ridViewCel lFormattin gEventArgs ) Handles datagrid.CellFormatting
'Check the column name you want look for to change color
If Me.datagrid.Columns(0).Nam e = "column1" Then
'Check if this is the right value
If e.Value < 0 Then
e.CellStyle.ForeColor = Color.Red
End If
End If
End Sub
What this does is goes through the datagrid and checks in that column for that particular instance, in this case if the value in the cell is less then 1, if it does then it changes the forecolor. I believe this is will work and I am going to change my application to do this.
Private Sub datagrid_CellFormatting(By
'Check the column name you want look for to change color
If Me.datagrid.Columns(0).Nam
'Check if this is the right value
If e.Value < 0 Then
e.CellStyle.ForeColor = Color.Red
End If
End If
End Sub
What this does is goes through the datagrid and checks in that column for that particular instance, in this case if the value in the cell is less then 1, if it does then it changes the forecolor. I believe this is will work and I am going to change my application to do this.
You can also use this to change the back color as well:
Private Sub datagrid_CellFormatting(By Val sender As Object, ByVal e As _ System.Windows.Forms.DataG ridViewCel lFormattin gEventArgs ) Handles datagrid.CellFormatting
'Check the column name you want look for to change color
If Me.datagrid.Columns(0).Nam e = "column1" Then
'Check if this is the right value
If e.Value < 0 Then
e.CellStyle.BackColor = Color.Red
End If
End If
End Sub
Private Sub datagrid_CellFormatting(By
'Check the column name you want look for to change color
If Me.datagrid.Columns(0).Nam
'Check if this is the right value
If e.Value < 0 Then
e.CellStyle.BackColor = Color.Red
End If
End If
End Sub
ASKER
ibost: I already came across this code, and my heart dropped when I realised that it was for web forms then. But thanks anyway.
jtaylor8181: I take it the CellFormatting event is part of ver2. As I have only ver1 (I'm hoping that the powers that be will give me 2), I don't have it. Or is there some other Namespace I need to import? My form only currently imports Data.SqlClient for my DB functions.
jtaylor8181: I take it the CellFormatting event is part of ver2. As I have only ver1 (I'm hoping that the powers that be will give me 2), I don't have it. Or is there some other Namespace I need to import? My form only currently imports Data.SqlClient for my DB functions.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!