C#重写DataGridView
作者:lijiao 发布时间:2021-06-09 11:56:26
标签:C#,重写,DataGridView
本文实例为大家分享了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);
}
}
}
}


猜你喜欢
- 之前使用springMVC+spring+mybatis,总是被一些繁琐的xml配置,有时候如果配置出错,还要检查各种xml配置,偶然接触到
- 一、 lib文件的简介.lib是一种文件后缀,是Windows操作系统的库文件,有静态lib和动态lib之分:1)、静态lib文件
- 1. 概述官方JavaDocsApi: java.awt.FlowLayoutFlowLayout,流式布局管理器。按水平方向依次排列放置组
- Java有四种访问权限,其中三种有访问权限修饰符,分别为private,public和protected,还有一种不带任何修饰符:1.&nb
- 一、Struts2文件上传 Struts2的文件上传实现非常简单,只需要简单几步就可完成;注意:(1)文件上传的struts2标签
- 前言C# 的编译器可以对代码进行优化,所以,我们在写代码的时候,可以更多地考虑一下代码的易读性问题。不考虑基本的对齐和换行美化。看一下局部变
- 什么是ContentType?我们知道浏览器可以处理各种各样的内容,比如:HTML、XML、JPG、Flash等等,那么浏览器是如何区分它们
- SpringMVC异常处理机制(一)项目前准备首先参照文章Spring课程工程构建+SpringMVC简介及其快速入门搭建项目搭建好一个项目
- 常用的Dialog有确认对话框,单选按钮对话框,多选按钮对话框,复选按钮对话框另外还有自定义的对话框AlertDialog的常用方法setT
- SpringBoot的web项目,在每一次修改了java文件或者是resource的时候,都必须去重启一下项目,这样的话浪费了很多的时间,实
- 前言我又搞回笃NET啦!java之路真是命运多舛,好事多磨。不过,也许我已经进入无招胜有招,博取众家之长、融会贯通的地步了。对于WebApi
- java连接zookeeper实现zookeeperJava服务端连接Zookeeper,进行节点信息的获取,管理…整理成一个基本工具添加依
- 1. openFeign实现基于spring-boot-starter-parent 2.6.8,spring-cloud-dependen
- 什么是分布式锁?在回答这个问题之前,我们先回答一下什么是锁。普通的锁,即在单机多线程环境下,当多个线程需要访问同一个变量或代码片段时,被访问
- 通过eclipse修改web的url访问路径今天做SpringMVC 基础跳转网页的时候发现了一个问题,就是eclipse访问url路径的问
- 我发现网上许多讲解javaweb 项目 SSM(Spring,SpringMVC,Mybatis)配置的时候有些重点没有提到,一下我会贴上一
- 前言对于字符串的操作,我们常用的就是trim()去除前后空格、subString()截取子字符串,其他的用的不多。下表中是字符串常用的方法。
- 本文实例讲述了C#多线程学习之操纵一个线程的方法。分享给大家供大家参考。具体实现方法如下:下面我们就动手来创建一个线程,使用Thread类创
- .c 源程序 ----- 编译 ----- 链接 ---- exe ----运行 -------->程序翻译环境和执行环境翻译环境:源
- 1. 引入jar包pom.xml文件<?xml version="1.0" encoding="UTF-