Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing the color of select datagrid rows 2

Status
Not open for further replies.

Billkamm

Programmer
Feb 9, 2006
74
US
Is there a way to change the color of a datagrid row if a certain condition is met other than placing a helper function in every column?
 
use the ItemDataBound event of the grid. then you can check your conditon and change the color accordingly.
Code:
      If <somecondition> Then
            e.Item.BackColor = System.Drawing.Color.Blue
        End If

Jim
 
You can add code such as this on the OnDataBindEvent:
Code:
Sub dgChemData_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then 
Dim strDO as String
strDO = DataBinder.Eval(e.Item.DataItem, "Oxygen")      
If strDO < 5 then ' oxygen..
 e.Item.Cells(4).Backcolor = System.Drawing.Color.Red
 e.Item.Cells(4).ForeColor = System.Drawing.Color.White  
End If
 
Although the principle is the same, I'd recommend changing the CssClass (e.Item.CssClass) rather than the BackColor otherwise these values will be hard coded and not particularly easy to change if you decide to visually update your site.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Excellent suggest ca8msm - in which case you can use the CSS file across many Grids and can be big timesaver. Billkamm you may take ca8msm up on his suggestion. Would you like a bit of code to show you how this would work? Let us know.
 
Thanks for the offer Isadore. I'm familiar with CSS already though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top