C#重写DataGridView
来源:本站原创|时间:2020-01-10|栏目:C#教程|点击: 次
本文实例为大家分享了C#重写DataGridView的实例代码,供大家参考,具体内容如下
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace CRD.WinUI.Editors { public class DataGridView : System.Windows.Forms.DataGridView { private bool _CellColorOnchange=false; private Color cell_color = Color.Yellow; private bool shifouhuasanjiao = true; private Color color_grid = Color.FromArgb(236, 233, 216); bool click = false; public DataGridView() { this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint, true); } protected override void OnCreateControl() { this.EnableHeadersVisualStyles = false; this.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(236, 233, 216); this.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised; //this.ColumnHeadersHeight = 20; 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 = Color.FromArgb(236, 233, 216); this.RowHeadersDefaultCellStyle.ForeColor = System.Drawing.SystemColors.WindowText; this.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Raised; this.DefaultCellStyle.SelectionBackColor = Color.DarkBlue; this.DefaultCellStyle.SelectionForeColor = Color.DarkSlateBlue; this.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing; //this.GridColor = Color.Silver;//表格点击后颜色 表格线颜色 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 { if (_CellColorOnchange) Rows[e.RowIndex].DefaultCellStyle.BackColor = cell_color; } catch (Exception) { } } //进入单元格时保存当前的颜色 protected override void OnCellMouseEnter(DataGridViewCellEventArgs e) { base.OnCellMouseEnter(e); try { if (_CellColorOnchange) defaultcolor = Rows[e.RowIndex].DefaultCellStyle.BackColor; } catch (Exception) { } } //离开时还原颜色 protected override void OnCellMouseLeave(DataGridViewCellEventArgs e) { base.OnCellMouseLeave(e); try { if (_CellColorOnchange) Rows[e.RowIndex].DefaultCellStyle.BackColor = defaultcolor; } catch (Exception) { } } public bool CellColorOnchange { get { return _CellColorOnchange; } set { _CellColorOnchange = value; } } public Color DefaultcolorSet { get { return cell_color; } set { cell_color = value; } } public bool Shifouhua_Sanjiao { get { return shifouhuasanjiao; } set { shifouhuasanjiao = value; } } public Color Content_Grid_color { get { return color_grid; } set { color_grid = value; } } private void InitializeComponent() { ((System.ComponentModel.ISupportInitialize)(this)).BeginInit(); this.SuspendLayout(); // // DataGridView // //this.RowTemplate.Height = 17; ((System.ComponentModel.ISupportInitialize)(this)).EndInit(); this.ResumeLayout(false); } //RowPostPaint protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e) { if (shifouhuasanjiao) { using (SolidBrush b = new SolidBrush(Color.Black)) { Image image = global::CRD.WinUI.Properties.Resources.未标题_1; //e.Graphics.DrawString("►", e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 5, e.RowBounds.Location.Y + 4); //e.Graphics.DrawImageUnscaled(image, e.RowBounds.Location.X + 1, e.RowBounds.Location.Y + 2, 8, 13); if (click) if (e.RowIndex == this.CurrentRow.Index) { e.Graphics.DrawImageUnscaled(image, e.RowBounds.Location.X + 1, e.RowBounds.Location.Y + 2, 8, 13); } } } base.OnRowPostPaint(e); } protected override void OnRowPrePaint(DataGridViewRowPrePaintEventArgs e) { if (shifouhuasanjiao) { using (SolidBrush b = new SolidBrush(Color.Black)) { Image image = global::CRD.WinUI.Properties.Resources.未标题_1; //e.Graphics.DrawString("►", e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 5, e.RowBounds.Location.Y + 4); //e.Graphics.DrawImageUnscaled(image, e.RowBounds.Location.X + 1, e.RowBounds.Location.Y + 2, 8, 13); } } base.OnRowPrePaint(e); } protected override void OnCellClick(DataGridViewCellEventArgs e) { if (e.RowIndex > -1&&this.CurrentRow.Index == e.RowIndex ) { click = true; } base.OnCellClick(e); } protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e) { base.OnCellPainting(e); SolidBrush b = new SolidBrush(Color.FromArgb(236, 233, 216)); Pen whitePen = new Pen(color_grid, 1); if (e.ColumnIndex == -1 && e.RowIndex == -1) { using (LinearGradientBrush brush = new LinearGradientBrush(e.CellBounds, Color.Gray, Color.Gray, LinearGradientMode.ForwardDiagonal)) { e.Graphics.FillRectangle(b, e.CellBounds); Rectangle border = e.CellBounds; border.Offset(new Point(-1, -1)); e.Graphics.DrawRectangle(Pens.Gray, border); } e.PaintContent(e.CellBounds); e.Handled = true; } else if (e.RowIndex == -1) { //标题行 using (LinearGradientBrush brush = new LinearGradientBrush(e.CellBounds, Color.Silver, Color.Silver, LinearGradientMode.Vertical)) { e.Graphics.FillRectangle(b, e.CellBounds); Rectangle border = e.CellBounds; border.Offset(new Point(-1, -1)); e.Graphics.DrawRectangle(Pens.Silver, border); //e.Graphics.DrawRectangle(Pens.Black, border.X + 1, border.Y + 1, border.Width - 1, border.Height - 1); } e.PaintContent(e.CellBounds); e.Handled = true; } else if (e.ColumnIndex == -1) { //标题列 using (LinearGradientBrush brush = new LinearGradientBrush(e.CellBounds, Color.Silver, Color.Silver, LinearGradientMode.Horizontal)) { e.Graphics.FillRectangle(b, e.CellBounds); Rectangle border = e.CellBounds; border.Offset(new Point(-1, -1)); e.Graphics.DrawRectangle(Pens.Silver, border); //e.Graphics.DrawRectangle(Pens.Black, border.X+1,border.Y+1,border.Width-1,border.Height-1); e.Graphics.DrawString("△", Font,b,e.CellBounds.X,e.CellBounds.Y); } e.PaintContent(e.CellBounds); e.Handled = true; } else { //Color.FromArgb(193, 193, 193) Rectangle border = e.CellBounds; border.Offset(new Point(-1, -1)); e.Graphics.DrawRectangle(whitePen, border); } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助。
栏 目:C#教程
下一篇:C#中Byte转换相关的函数
本文标题:C#重写DataGridView
本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/6549.html
您可能感兴趣的文章
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10C#停止线程的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的方法
- 01-10C#实现清空回收站的方法
- 01-10C#实现多线程下载文件的方法
- 01-10C#实现Winform中打开网页页面的方法
- 01-10C#实现远程关闭计算机或重启计算机的方法
- 01-10C#自定义签名章实现方法
- 01-10C#文件断点续传实现方法
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频 器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 01-11ajax实现页面的局部加载
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 04-02jquery与jsp,用jquery
- 01-10SublimeText编译C开发环境设置
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法实例总结
- 01-10使用C语言求解扑克牌的顺子及n个骰子