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

Accessing DataGrid ItemStyle BackColor Property 2

Status
Not open for further replies.

TomTT

Programmer
Jun 3, 2002
55
US
This may be obvious, but it eludes me. I want to capture the row color or alternating row color of a datagrid during the ItemCreated event.

The event handler looks like:

Sub dgData_ItemCreated(sender As Object, e As DataGridItemEventArgs)

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

This is where I lose it. syntax like e.item.backcolor or ListItemType.Item.BackColor or e.Item.Properties all cause errors. I'm sure I can access these properties, but can't quite figure the syntax.

End If
End Sub

Suggestions would be appreciated.

Thanks
Tom T
 
Here the attributes are setting mouse-overs, the syntax might help.

Sub dgGroups_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
'Add the OnMouseOver and OnMouseOut method to the Row of DataGrid
e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#CCFFFF'")
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='White'")
End If
End Sub
 
Thanks Isadore.

I have that code and have looked at it trying to figure out how to convert it to a syntax I can use to set the row color. I guess I need to figure out an event other than mouseover or mouseout to hang the change on.

There should be some kind of direct way (ie without javascript) to just change the backcolor property of the datagrid row.

I'll keep plugging away and post it if I find the secret.

Thanks again,
Tom T
 
Tom --

The alternating routine is out there, I've seen it before; just don't have it handy. I'll take a few minutes here shortly and see if I can't run it down. I remember its a pretty simple statement, includes style, formatting, etc.

Hang in there -- if I find it; I'll post back...
 
Thanks for searching. I have found a syntax that works, but it looks way too clunky to be commonly used.

If I put the following in the OnItemCreated event handler, I can get the row color changed:

e.Item.backcolor=system.Drawing.ColorTranslator.FromOle(&H99ffff&)

I figure there has to be a simpler approach than this.

Thanks,
Tom T
 
I remember the solution I saw Tom wasn't much different at all, don't remember the Translator -- but you must be very close -- keep my eyes open.
 
I finally found the basic solution to setting the datagrid row colors.

In the onItemCreated event handler the following works:

e.Item.BackColor = System.Drawing.Color.aqua

The limiting factor is that it use the system drawing preset colors which are somewhat limited and called by name instead of value.
 
TomTT -- good work, can always use a dgrid tool -- good luck to ya.
 
You can use the Color.FromArgb(int, int, int) static method, to return a Color object representing the colour you require. And just because I found it helpful, the Convert.ToString and Convert.ToInt32 (iirc) methods can take optional bases to convert to (for dec to hex conversion) which you might need.
 
Thanks to both nooro and Isadore.

I started the post not knowing how to change the datagrid backcolor at runtime and now have three methods!

I really appreciate you taking the time to provide responses.

Regards,
Tom T

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top