If one needs to dynamically alter the text color in a GridView Row based on some value of one of the columns in that row, what is the best way to do that? I would assume one would use the OnRowDataBound or OnRowCreated events?
Using RowDataBound is frequently the best way. Here's a quick example that changes the row color based on the status of a CheckBoxField.
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If (e.Row.RowType = DataControlRowType.DataRow) Then
Dim cb As CheckBox = CType(e.Row.Cells(1).Controls(0), CheckBox)
If (cb.Checked = False) Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(0).ForeColor = Drawing.Color.Red
Else
e.Row.ForeColor = Drawing.Color.Blue
cb.Enabled = True
End If
End If
End Sub
Hope that helps.
Aaron
Don't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so everyone will know you have been helped.
I'm looking to do something similar. My GridView is not bound, but dynamically created based upon a selection of a DropDownList control that passes a parameter to my SQL Stored Procedure. All data returned to the GridView is read only. However, column 3
(data type bit) of the GridView is a greyed-out checkbox indicating weather the item is authorized or not. I want the background color of that row to be red if it is not authorized.
I am just learning and do not fully understand everything that is happening in the code you provided.
Sorry for jumping in, but thanks for getting me to look in this direction.
No problem. I've repeated the code with comments to help make some sense out of it (if it doesn't, let me know).
' For each row of the GridView RowDataBound is called after the row is created and data is bound to the row.
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
' RowDataBound is called for each row, including the header/footer, so need to check that this is a DataRow (if it's
' not, there's no point searching for a checkbox that doesn't exist).
If (e.Row.RowType = DataControlRowType.DataRow) Then
' For any column that's a CheckBoxField, a Checkbox is created in the Controls collection for that Cell of the Row.
' Need to get a reference to that CheckBox to check its properties.
Dim cb As CheckBox = CType(e.Row.Cells(1).Controls(0), CheckBox)
If (cb.Checked = False) Then
' If the CheckBox is not checked, change the background color for the whole row and the text color for only the
' first cell.
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(0).ForeColor = Drawing.Color.Red
Else
' If the CheckBox is checked, change the text color for the whole row and enable the CheckBox (the default
' for a DataRow is disabled).
e.Row.ForeColor = Drawing.Color.Blue
cb.Enabled = True
End If
End If
End Sub
Hope that helps.
Aaron
Don't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so everyone will know you have been helped.
That worked GREAT Aaron. Thank you! If I may, can I expan on this? I foresee the need to evluate adate or text field. For the date, I would want to evaluate dates > 365 and mark them one color, and dates > 335 but < 365 to be a different color. The text
field would just be a standard compare.
NOTE:If you find my response contains a reference to a third party World Wide Web site, I am providing this information as a convenience to you.Microsoft does not control these sites and has not tested any software or information found on these sites; therefore,Microsoft cannot make any representations regarding the quality,safety, or suitability of any software or information found there.
__________________________________________________
Sincerely,
Young Fang
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
mcs130
Member
38 Points
52 Posts
Dynamically change color of text in a particular GridView row
Apr 28, 2007 05:42 PM|LINK
If one needs to dynamically alter the text color in a GridView Row based on some value of one of the columns in that row, what is the best way to do that? I would assume one would use the OnRowDataBound or OnRowCreated events?
Thanks
agolden
Star
7893 Points
1060 Posts
Re: Dynamically change color of text in a particular GridView row
Apr 28, 2007 09:57 PM|LINK
Hope that helps.
Aaron
paskettj
Member
376 Points
207 Posts
Re: Dynamically change color of text in a particular GridView row
Apr 29, 2007 04:54 PM|LINK
Aaron,
I'm looking to do something similar. My GridView is not bound, but dynamically created based upon a selection of a DropDownList control that passes a parameter to my SQL Stored Procedure. All data returned to the GridView is read only. However, column 3 (data type bit) of the GridView is a greyed-out checkbox indicating weather the item is authorized or not. I want the background color of that row to be red if it is not authorized.
I am just learning and do not fully understand everything that is happening in the code you provided.
Sorry for jumping in, but thanks for getting me to look in this direction.
<JP>
agolden
Star
7893 Points
1060 Posts
Re: Dynamically change color of text in a particular GridView row
Apr 30, 2007 12:11 AM|LINK
Hi JP,
No problem. I've repeated the code with comments to help make some sense out of it (if it doesn't, let me know).
Hope that helps.
Aaron
paskettj
Member
376 Points
207 Posts
Re: Dynamically change color of text in a particular GridView row
Apr 30, 2007 12:24 PM|LINK
That worked GREAT Aaron. Thank you! If I may, can I expan on this? I foresee the need to evluate adate or text field. For the date, I would want to evaluate dates > 365 and mark them one color, and dates > 335 but < 365 to be a different color. The text field would just be a standard compare.
Thanks again! <JP>
Young Fang -...
All-Star
17147 Points
1620 Posts
Re: Dynamically change color of text in a particular GridView row
May 03, 2007 06:39 AM|LINK
Hi paskettj,
Sure. You can expand this,like this.
__________________________________________________
Sincerely,
Young Fang
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
djbprogramme...
Member
2 Points
1 Post
Re: Dynamically change color of text in a particular GridView row
May 26, 2009 02:39 PM|LINK
This code snippet was very helpful. Thank you for taking the time to post it.