• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#重写DataGridView

C#重写DataGridView

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了马桶c的个人空间,c语言,欲情 c max,维生素c,奔驰c200等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

 本文实例为大家分享了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.Si



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C#利用ReportViewer生成报表
  • C#基于正则去掉注释的方法示例
  • C#中new的用法及与override的区别分析
  • C#实现两个richtextbox控件滚动条同步滚动的简单方法
  • C# for循环的经典案例集锦
  • C#操作word的方法示例
  • C#使用WebClient登录网站并抓取登录后的网页信息实现方法
  • C# WinForm制作异形窗体与控件的方法
  • C#实现Excel表数据导入Sql Server数据库中的方法
  • C#使用NPOI上传excel

相关文章

  • 2017-05-28C#中调用命令行cmd开启wifi热点的实例代码
  • 2017-05-28C# 泛型参数转换
  • 2017-05-28C#中数组、ArrayList和List三者的区别详解及实例
  • 2017-05-28c#使用微信接口开发微信门户应用
  • 2017-05-28c#设计模式 适配器模式详细介绍
  • 2017-05-28简单谈谈C#中深拷贝、浅拷贝
  • 2017-05-28asp.net core mvc权限控制:在视图中控制操作权限
  • 2017-05-28C#实现HTTP上传文件的方法
  • 2017-05-28c#实现摄像头拍照功能示例
  • 2017-05-28C#传值方式实现不同程序窗体间通信实例

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • WPF设置窗体可以使用鼠标拖动大小的方法
    • C#中无边框窗体移动的简单实例
    • C#之WinForm WebBrowser实用技巧汇总
    • 理解C#中的枚举(简明易懂)
    • C#6.0中10大新特性的应用和总结
    • Windows系统中C#调用WinRAR来压缩和解压缩文件的方法
    • C#编程获取资源文件中图片的方法
    • C#实现在启动目录创建快捷方式的方法
    • C#向word文档插入新段落及隐藏段落的方法
    • C#获取Windows进程监听的TCP/UDP端口实例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有