C#重写DataGridView

 更新时间:2016年05月04日 16:09:52   投稿:lijiao  
这篇文章主要为大家详细介绍了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);
      }
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助。

相关文章

  • c#判断网络连接状态的示例分享

    c#判断网络连接状态的示例分享

    这篇文章主要介绍了使用c#判断网络连接状态的示例,需要的朋友可以参考下
    2014-02-02
  • C#实现简单的五子棋游戏

    C#实现简单的五子棋游戏

    这篇文章主要为大家详细介绍了C#实现简单的五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-01-01
  • C#使用控制台列出当前所有可用的打印机列表

    C#使用控制台列出当前所有可用的打印机列表

    这篇文章主要介绍了C#使用控制台列出当前所有可用的打印机列表,涉及C#操作计算机硬件的相关使用技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • Winform中实现图片格式转换

    Winform中实现图片格式转换

    这篇文章主要介绍了Winform中实现图片格式转换的示例代码,帮助大家更好的理解和使用winform开发,感兴趣的朋友可以了解下
    2020-12-12
  • C# Razor语法规则

    C# Razor语法规则

    这篇文章介绍了C# Razor的语法规则,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-01-01
  • WinForm实现读取Resource中文件的方法

    WinForm实现读取Resource中文件的方法

    这篇文章主要介绍了WinForm实现读取Resource中文件的方法,很实用的一个功能,需要的朋友可以参考下
    2014-08-08
  • C#将html table 导出成excel实例

    C#将html table 导出成excel实例

    C#将html table 导出成excel实例,需要的朋友可以参考一下
    2013-04-04
  • C#使用Resources资源文件

    C#使用Resources资源文件

    这篇文章介绍了C#使用Resources资源文件的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • C# listview添加combobox到单元格的实现代码

    C# listview添加combobox到单元格的实现代码

    从别处转来的,自己进行了一些小的修改,还不错,你自己先拖一个ListView1和一个ComboBox1,需要的朋友可以参考下
    2014-06-06
  • 详解C++中类的大小决定因数

    详解C++中类的大小决定因数

    类的大小受多个因素影响,主要包括成员变量、对齐方式、继承关系、虚函数表等,下面就来介绍一下,具有一定的参考价值,感兴趣的可以了解一下
    2025-04-04

最新评论