Wednesday, November 10, 2004

highlight row in datagrid

resource link:
http://www.dotnetjohn.com/articles.aspx?articleid=12

because datagrid is not like repeater, its row element is not accessbile at design time. so I can't not set onmouseover event to the TD or TR at design time. But I could use datagrid's OnItemDataBound event handler to deal with this.

if (e.Item.ItemType == ListItemType.Item e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView row = (DataRowView) e.Item.DataItem;
#region Set MouseOver/MouseOut
e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#D6DEEC'");
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff'");
#endregion
......

in this way, I can even have the datagrid highlight same color but change back to different color when mouseout based on this row's ItemType (Item or AlternatingItem).

No comments: