为dataGridView增加一个鼠标经过时高亮的效果

 

我们在做项目时,记得要用自己的控件,

 

不要直接就用微软的控件,这样子就算有什么东西要增加修改时,

 

也可以做到统一修改和增加

 

现在就实现继承dataGridView鼠标经过时高亮显示

 

代码如下

 

 

  class GridView : System.Windows.Forms.DataGridView
    {
        #region Andy' Code

        #region 属性代码
        /// <summary>
        /// 进入Cells之前的颜色
        /// </summary>
        private static System.Drawing.Color _BeforInCellsColor;
        /// <summary>
        /// 鼠标经过时的高亮颜色
        /// </summary>
        private static System.Drawing.Color _MouseOverHighColor = System.Drawing.Color.SkyBlue;
        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.Description("鼠标经过时的高亮颜色")]
        [System.ComponentModel.DefaultValue(typeof(System.Drawing.Color), "SkyBlue")]
        [System.ComponentModel.Category("OtherStyle")]
        public System.Drawing.Color MouseOverHighColor
        {
            get { return GridView._MouseOverHighColor; }
            set { GridView._MouseOverHighColor = value; }
        }

        /// <summary>
        /// 是否启用鼠标经过时颜色高亮,如启用,SelectionMode也会被设为FullRowSelect
        /// </summary>
        private bool _UseMouseOverStyle = false;

        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.Category("OtherStyle")]
        [System.ComponentModel.Description("是否启用鼠标经过时颜色高亮,如启用,SelectionMode也会被设为FullRowSelect")]
        [System.ComponentModel.DefaultValue(false)]
        public bool UseMouseOverStyle
        {
            get { return _UseMouseOverStyle; }
            set
            {
                if (value)
                    this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                else
                    this.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
                _UseMouseOverStyle = value;
            }
        }

        private static Color _BeforCellSelect;

        #endregion

        #region 重写的方法

        protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e)
        {
            base.OnDataBindingComplete(e);
            if (this.Rows.Count > 0)
            {
                for (int i = 0; i < this.Rows.Count; i++)
                {
                    this.Rows[i].HeaderCell.Value = (i + 1).ToString();
                }
                if (this.SelectedRows.Count > 0)
                    SelectedRows[0].Selected = false;
            }
        }

        protected override void OnCellMouseLeave(DataGridViewCellEventArgs e)
        {
            base.OnCellMouseLeave(e);
            if (UseMouseOverStyle)
            {
                if (e.RowIndex > -1 && e.ColumnIndex > -1)
                {
                    if (!this.Rows[e.RowIndex].Selected)
                    {
                        this.Rows[e.RowIndex].DefaultCellStyle.BackColor = _BeforInCellsColor;
                    }
                }
            }
        }

        protected override void OnCellMouseEnter(DataGridViewCellEventArgs e)
        {
            base.OnCellMouseEnter(e);
            if (UseMouseOverStyle)
            {
                if (e.RowIndex > -1 && e.ColumnIndex > -1)
                {
                    if (!this.Rows[e.RowIndex].Selected)
                    {
                        _BeforInCellsColor = this.Rows[e.RowIndex].DefaultCellStyle.BackColor;
                        this.Rows[e.RowIndex].DefaultCellStyle.BackColor = _MouseOverHighColor;
                    }
                }
            }
        }

        protected override void OnRowLeave(DataGridViewCellEventArgs e)
        {
            base.OnRowLeave(e);
            this.Rows[e.RowIndex].DefaultCellStyle.BackColor = _BeforCellSelect;
        }

        protected override void OnCellClick(DataGridViewCellEventArgs e)
        {
            base.OnCellClick(e);
            _BeforCellSelect = _BeforInCellsColor;
        }
      

        #endregion

       
        #endregion       
    }

 

记得添加这些引用

using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Reflection;

 

完成后生成一下项目,就会在工具栏生成一个组件,

 

可以直接拉来用了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值