C#重写DataGridView

作者:lijiao 时间:2021-06-09 11:56:26 

 本文实例为大家分享了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#,重写,DataGridView
0
投稿

猜你喜欢

  • 详解java_ 集合综合案例:斗地主

    2022-02-08 04:14:49
  • Java 动态代理的多种实现方式

    2023-11-20 05:23:14
  • spring 注解如何开启声明式事务

    2023-04-09 23:07:42
  • Android实现购物车功能

    2022-08-14 19:06:55
  • C#学习笔记整理_浅谈Math类的方法

    2022-04-11 06:16:48
  • Kotlin 基础教程之数组容器

    2021-09-09 17:22:40
  • IDEA maven项目中刷新依赖的两种方法小结

    2022-10-06 05:52:03
  • Spring中集成Groovy的四种方式(小结)

    2023-07-11 16:36:53
  • springboot +rabbitmq+redis实现秒杀示例

    2022-04-21 02:29:28
  • SpringBoot整合activemq的案例代码

    2023-11-06 18:41:37
  • adb通过wifi连接android设备流程解析

    2021-12-27 08:39:37
  • 解析Android获取系统cpu信息,内存,版本,电量等信息的方法详解

    2023-11-20 10:50:15
  • C#设置文件权限的方法

    2022-08-31 22:35:42
  • log4j配置失效日志中打印Debug信息问题

    2022-10-12 10:13:12
  • log4j使用教程详解(怎么使用log4j2)

    2022-06-18 23:48:47
  • C#如何调用MFC 窗口 DLL

    2022-04-14 09:57:51
  • 详解MyBatis-Plus Wrapper条件构造器查询大全

    2023-09-05 08:55:52
  • Android Studio 多层级 Module 对 aar 引用问题解决方法

    2023-08-06 19:41:27
  • JavaBean和SpringBean的区别及创建SpringBean方式

    2022-05-23 03:32:14
  • C# 迭代器分部类与索引器详情

    2023-10-16 21:18:18
  • asp之家 软件编程 m.aspxhome.com