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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting backcolor using itemdatabound 1

Status
Not open for further replies.

ethorn10

Programmer
Feb 18, 2003
406
US
Hello,

I cannot figure this out for the life of me and it can't possibly be this difficult. I have a datagrid and I'm trying to set the backcolor of a specific cell based on the text of that cell (not that uncommon I presume). My code:
Code:
Sub ItemDataBound(ByVal Sender As Object, ByVal E As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dtgResults.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            If e.Item.Cells(7).Text = "no" Then
                e.Item.Cells(7).BackColor = System.Drawing.Color.Red
            End If
        End If
End Sub

The crazy thing is, if I change the
Code:
e.Item.Cells(7).BackColor = System.Drawing.Color.Red
to
Code:
e.Item.Cells(7).Text = "wtf???"
it prints just fine so it's obviously getting inside there but the cell isn't changing to red. It maintains its boring white backcolor. Any help is greatly appreciated.
 
The string comparison is case sensitive. Make sure the value is actually "no" not "No" or "NO" in the cell.

Jim

 
Thanks jbenson...

however, the comparison definitely matches because I get inside that "IF" statement and can successfully change the .Text = "wtf??" but I can't get the .BackColor = System.Drawing.Color.Red to work. It's extremely odd.
 
Yes, it is odd. I just did a test page and it works fine. Are you setting the bgcolor somewhere else or using CSS?

Jim
 
Have a look at the View Source of your page for the rendered DataGrid. If it doesn't appear to be writing any attributes out for the row then there's a bit of a problem as it seems to work fine for me (and for Jim as well).

You could aslo try assigning a CSS class to the relevant item which is a much better approach anyway.


____________________________________________________________

Need help finding an answer?

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

 
Didn't touch a thing overnight and now it's working. I quit. Some errors I can handle but stuff like this just drives me mad.
 
It may have been something as simple as the browser showing a cached page. Try clearing all the temporary files if you get another problem like this.


____________________________________________________________

Need help finding an answer?

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

 
Thanks for the input guys. I'll keep this in mind next time...obviously a hard refresh (ctrl+F5) doesn't quite cut it. Thanks again.
 
Upon further review of this quirk...it appears to be a CSS thing after all. It looks like Firefox cares that I'm setting the color in the CSS and this overwrites anything I do in the code-behind but IE seems to say "meh, whatever you wanna do" and the code-behind overwrites the CSS. Looking at it on the same machine using both browsers with cache cleared, it shows up red in IE but white in Firefox. Again...thanks for the help guys.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top