GridView:当鼠标滑过,行的背景颜色,鼠标指针发生变化。

前提条件:GridView已经能正常的显示数据了!
这里我将NorthWind数据库的Category表显示出来,接着我们要是实现以下三个功能:
1、将GridView中满足CategoryID为偶数的数据行背景色改为Silver;
2、当鼠标滑过GridView中的数据行是,该数据行的颜色变为黄色,且数据字体加粗。鼠标离开行时,还原初始状态;
3、当点击GridView中的行时,行的颜色花生变化,且鼠标的形状变为手型。
这里我们通过定义GridView的 RowDataBound事件来实现以上三个功能。GridView.RowDataBound事件是在 GridView 控件中将数据行绑定到数据时发生。
在其中添加以下代码:
//这里我们将对NorthWind数据库的Category表进行操作
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
     //将CategoryID为偶数的行设为银色(Silver)
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
        //判定当前的行是否为数据行(即类型是否为DataRow)
        int cid = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "CategoryID"));
        //获取当前行的CategoryID列的值
        if (cid % 2 == 00)
            e.Row.BackColor = Color.Silver;
   }

    //设置鼠标滑过,行变色的效果
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
        //当鼠标放上去的时候 先保存当前行的背景颜色 并设置新的背景色
        e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='yellow'; this.style.fontWeight='bold';");
        //当鼠标离开的时候 将背景颜色恢复成之前的颜色
        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor; this.style.fontWeight='';");
   }

     //设置鼠标点击,行变色、鼠标指针变成手状的效果
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
        e.Row.Attributes.Add("onclick", "this.style.backgroundColor='#99cc00'; this.style.cursor='hand';");
   }  
}

第01个小程序:随鼠标移动而变色的背景(VaryTheBackground.cs) 程序运时,当鼠标指针靠近窗口中心程度不同,客户区的背景颜色也不同。 using System; using System.Windows; using System.Windows.Input; using System.Windows.Media; namespace Chapter02 { public class VaryTheBackground : Window { SolidColorBrush brush = new SolidColorBrush(Colors.Black); //创建画刷 。SolidColorBrush是最简单的画刷。 [STAThread] public static void Main() { Application app = new Application(); app.Run(new VaryTheBackground()); } public VaryTheBackground() { Title = "Vary the Background"; Width = 384; Height = 384; Background = brush; } //鼠标移动事件 protected override void OnMouseMove(MouseEventArgs args) { //窗口呈现尺寸-边框和标题栏的尺寸 double width = ActualWidth - 2 * SystemParameters.ResizeFrameVerticalBorderWidth; double height = ActualHeight - 2 * SystemParameters.ResizeFrameHorizontalBorderHeight - SystemParameters.CaptionHeight; Point ptMouse = args.GetPosition(this); //获取鼠标位置 Point ptCenter = new Point(width / 2, height / 2); //窗口中心 Vector vectMouse = ptMouse - ptCenter; //向量。向:中心指向鼠标位置;量:鼠标位置与中心距离 double angle = Math.Atan2(vectMouse.Y, vectMouse.X); Vector vectEllipse = new Vector(width/ 2 * Math.Cos(angle), height / 2 * Math.Sin(angle)); Byte byLevel = (byte)(255 * (1 - Math.Min(1, vectMouse.Length / vectEllipse.Length))); Color clr = brush.Color; clr.R = clr.G = clr.B = byLevel; brush.Color = clr; } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值