b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

datagridview 美化_datagridview绑定list_datagridview添加行

电脑杂谈  发布时间:2017-05-30 14:10:37  来源:网络整理
datagridview 美化datagridview 美化

C#皮肤—更新DataGridView的鼠标跟随效果

导读部分

-------------------------------------------------------------------------------------------------------------

C#皮肤-实现原理系列文章导航

我们先来看看更新后的效果吧

其实方法很简单我们只要重写一下几个事件就行了,首先是OnCellMouseEnter事件,这个事件的做用只有一个就是保存当前的行背景色,我们都知道我们鼠标移到上方时要改变颜色,那移过后是不是还原呢,不然就成了染布了,呵呵,这个事件的做用就是记录和保存颜色一起看实现

[code=csharp] //进入单元格时保存当前的颜色

protected override void OnCellMouseEnter(DataGridViewCellEventArgs e)

{

base.OnCellMouseEnter(e);

try

{

defaultcolor = Rows[e.RowIndex].DefaultCellStyle.BackColor;

}

catch (Exception)

{

}

}[/code]

defaultcolor是一个颜色变量用来保存默认颜色

[code=csharp]Rows[e.RowIndex].DefaultCellStyle.BackColor;[/code]

是取得当前的默认颜色

这样每次在改变颜色之前就可以保存默认颜色了那就是defaultcolor 大家一定要记着不能把这一步写在OnCellMouseMove事件里,一定要写在OnCellMouseEnter事件里,那是因为OnCellMouseEnter在OnCellMouseMove事件之前执行,否则是取不到默认颜色的就成了染布,呵呵

那我们再来看OnCellMouseMove事件吧,它的作用只有一个移到单元格时的改变颜色

[code=csharp] //移到单元格时的颜色

protected override void OnCellMouseMove(DataGridViewCellMouseEventArgs e)

{

base.OnCellMouseMove(e);

try

{

Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.YellowGreen;

}

catch (Exception)

{

}

}[/code]

拉下来就是用OnCellMouseLeave事件还原默认颜色了

[code=csharp]//离开时还原颜色

protected override void OnCellMouseLeave(DataGridViewCellEventArgs e)

{

base.OnCellMouseLeave(e);

try

{

Rows[e.RowIndex].DefaultCellStyle.BackColor = defaultcolor;

}

catch (Exception)

{

}

}[/code]

好了到这到这里工作就完成了,所有代码如下

[code=csharp]代码

using System;

using System.Collections.Generic;

using System.Text;

using System.Drawing;

using System.Windows.Forms;

namespace CRD.WinUI.Editors

{

public class DataGridView : System.Windows.Forms.DataGridView

{

public DataGridView()

{

this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);

}

protected override void OnCreateControl()

{

this.EnableHeadersVisualStyles = false;

this.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(247, 246, 239);

this.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;

this.ColumnHeadersHeight = 26;

this.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;

this.ColumnHeadersDefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;

this.ColumnHeadersDefaultCellStyle.ForeColor = System.Drawing.SystemColors.WindowText;

this.ColumnHeadersDefaultCellStyle.SelectionBackColor = System.Drawing.SystemColors.Highlight;

this.ColumnHeadersDefaultCellStyle.SelectionForeColor = System.Drawing.SystemColors.HighlightText;

this.RowHeadersDefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;

this.RowHeadersDefaultCellStyle.BackColor = System.Drawing.SystemColors.Window;

this.RowHeadersDefaultCellStyle.ForeColor = System.Drawing.SystemColors.WindowText;

this.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;

this.DefaultCellStyle.SelectionBackColor = Color.Wheat;

this.DefaultCellStyle.SelectionForeColor = Color.DarkSlateBlue;

this.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;

this.GridColor = System.Drawing.SystemColors.GradientActiveCaption;

this.BackgroundColor = System.Drawing.SystemColors.Window;

this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

this.AllowUserToOrderColumns = true;

this.AutoGenerateColumns = true;

base.OnCreateControl();

}

Color defaultcolor;

//移到单元格时的颜色

protected override void OnCellMouseMove(DataGridViewCellMouseEventArgs e)

{

base.OnCellMouseMove(e);

try

{

Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.YellowGreen;

}

catch (Exception)

{

}

}

//进入单元格时保存当前的颜色

protected override void OnCellMouseEnter(DataGridViewCellEventArgs e)

{

base.OnCellMouseEnter(e);

try

{

defaultcolor = Rows[e.RowIndex].DefaultCellStyle.BackColor;

}

catch (Exception)

{

}

}

//离开时还原颜色

protected override void OnCellMouseLeave(DataGridViewCellEventArgs e)

{

base.OnCellMouseLeave(e);

try

{

Rows[e.RowIndex].DefaultCellStyle.BackColor = defaultcolor;

}

catch (Exception)

{

}

}

}

}[/code]

只要我们生成一下就行了,然后项目的各个地方 就会要着变了,不需要动任何代码就OK,其实这也是自定义控件的好处,只要我们重写一下控件 ,就要吧让系统涣然一新了,最新版的源代码已经上传了

,请大家到导航页面去下载最新版本 因为有点烂没有写成属性的方式,大家如果感觉颜色不好看可以自己改改,我会在下次更新时改成属性的方式 ,让大家在直接选择颜色,,,,,,,,,,,,,,


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-50118-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      • 千叶千惠巳
        千叶千惠巳

        有十万的人不会在乎那100

      • 朱博然
        朱博然

        上午10点半飞过广场后各自回场

      热点图片
      拼命载入中...