经过一个月来的学习,我总结了用过的几个有关于DataGrid的小技巧:
1. 为每列绑定相应的ToolTip,让其显示相关的信息。(如图)
实现代码:
private void O_dataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType == ListItemType.Item)
{
WebControl V_wc = (WebControl)e.Item.Cells[1].Controls[0];
V_wc.ToolTip = "库存量为 "+e.Item.Cells[4].Text+" 个单位";
}
}
2.鼠标移动到某一行动态显示不同的颜色。(如上图)
private void O_dataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType == ListItemType.Item)
{
e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='#FFC66C'");
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor='#FFFFFF'");
}
}
3. 如果DataGrid的项是用样式来确定显示颜色的,代码为:
private void O_dataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType == ListItemType.Item)
{
e.Item.Attributes.Add("onmouseover",
"javascript:this.oldClassName=this.className;this.className='Over'");
e.Item.Attributes.Add("onmouseout",
"javascript:this.className=this.oldClassName");
}
}