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);
      }
    }
  }
}

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

(0)

相关推荐

  • 在ASP.NET 2.0中操作数据之十:使用 GridView和DetailView实现的主/从报表

    导言 在前面的教程我们看到了如何使用两个页面(一个主页,用于列出供应商; 一个明细页,用于显示选定供应商提供的产品)创建主/从报表 . 这种两个页面的报表格式也可以集中在一个页面上. 这篇教程将会使用一个GridView, 它的每一行都包含产品的名称和单价以及一个选择按钮. 单击一个产品的选择按钮会在同一页的DetailsView控件上显示该产品的全部详细信息. 图 1: 单击选择按钮显示产品明细 Step 1: 创建一个可选择行的GridView 回想一下前面的跨页的主/从报表, 它的每个主记

  • ASP.NET 2.0中的数据操作之七:使用DropDownList过滤的主/从报表

    导言 主/从报表是一种很常见的报表, 这类报表中首先会显示一些主记录. 然后用户可以深入(追溯)某条主记录来查看该主记录的详情. 主/从报表是显示一对多关系的理想选择, 比如一个报表显示所有的产品类别,然后根据用户选择的特定类别显示与之关联的产品. 另外, 主/从表在显示"宽"表(有很多列的表)的详细信息时也很有用.例如主/从报表的主表部分显示数据库中产品表的产品名称和单价, 具体到某一个产品时再显示其他的产品字段(类别,供应商,单位数量,等等). 有很多方法可以实现主/从报表. 在这

  • ASP.NET 2.0中的数据操作之八:使用两个DropDownList过滤的主/从报表

    导言 在前面的指南中我们研究了如何显示一个简单的主/从报表, 该报表使用DropDownList和GridView控件, DropDownList填充类别,GridView显示选定类别的产品. 这类报表用于显示具有一对多关系的记录时非常合适, 同时它也可以很容易的被扩展以显示多个一对多关系的数据. 比如, 一个订单系统应该包含表示客户,订单和订单明细的表. 一个客户也许有多个订单,每个订单又包含多条订单项. 这样的数据可以使用两个DropDownList和一个GridView呈现给用户. 第一个

  • ASP.NET数据绑定之GridView控件

    GridView 是 DataGrid的后继控件,在.net framework 2 中,虽然还存在DataGrid,但是GridView已经走上了历史的前台,取代DataGrid的趋势已是势不挡.  作用:其功能是在web页面中显示数据源中的数据.GridView和DataGrid功能相似,都是在web页面中显示数据源中的数据,将数据源中的一行数据,也就是一条记录,显示为在web页面上输出表格中的一行.     在此GirdView的详细属性和事件我不再阐述.下面我只是简单介绍一下GirdVi

  • asp.net gridview分页:第一页 下一页 1 2 3 4 上一页 最末页

    效果图: 功能简介:可使用上下键选中行,选中后点击修改,textbox获得gridview中的代码的数据.对你有帮助的话,请记得要点击"好文要顶"哦!!!不懂的,请留言.废话不多说了,贴码如下: <head runat="server"> <title>GridView分頁</title> <script type="text/javascript"> var currentRowId = 0; v

  • 在ASP.NET 2.0中操作数据之六:编程设置ObjectDataSource的参数值

    导言 正如我们在上一节的教程中所看到的,有很多可供选择的方式把参数的值传递到OjbectDataSource的方法里.假如参数值是采用硬编码方式,来源于页面上的一个Web控件,又或者其他可被数据源Parameter对象读取的源,那么这个值可以绑定到输入参数而不需要写一行的代码. 然而有些时候,参数值来自某些在数据源的内置Parameter对象里还没有计算出来的源.假如我们的站点支持我们的考虑那么我们也许希望参数基于当前登录用户.又或者我们在参数传送到ObjectDataSource的隐含对象的方

  • 在ASP.NET 2.0中操作数据之五:声明参数

    导言 在上一章的教程中,我们看了GridView.DetailsView和FormView绑定到OjbectDataSource控件显示数据,ObjectDataSource调用了类ProductsBLL的GetProducts()方法.方法GetProducts()返回一个有Northwind数据库的Products表的所有记录组成的强类型数据表.类ProductsBLL还包含了其它返回部分数据的方法:GetProductByProductID(productID),GetProductsBy

  • ASP.NET数据绑定GridView控件使用技巧

    不得不说GridView控件的功能确实很强大,一个简简单单的控件就可以把数据管理的很美.在这两天做的任务中碰到的一些GridView控件中遇到的问题进行总结: ①:在GridView控件中随意显示数据库中的信息: GridView控件中有一个AutoGenerateColumns属性,它的作用就是控制GridView控件是否在运行的时候自动生成相关联的列,一般情况下把这个属性设置成为false.因为我们需要的是一个DIY的GridView控件.然后点击右上角的箭头,选择编辑列添加一个BoundF

  • ASP.NET 2.0中的数据操作之九:跨页面的主/从报表

    导言 在前面的两篇教程中,我们看到了如何在单一页面中显示主/从报表, 它使用DropDownList显示主记录,使用GridView或DetailsView显示详细信息. 另外一种常见的主/从报表模式是在一个页面中显示主记录而在另一个页面中显示详细信息.互联网上的论坛,如www.asp.net ,就是该模式在实际应用中非常典型例子. Asp.Net论坛由多个子论坛组成: Getting Started, Web Forms, Data Presentation Controls 等等. 每个子论

  • 在ASP.NET 2.0中操作数据之四:使用ObjectDataSource展现数据

    在完成了应用程序架构和页面的设计后,我们可以开始探讨如何实现多种多样的普通数据到报表展现的任务.前面我们已经了解到如何在从数据访问层和业务逻辑层绑定数据到ASP.NET页面上的数据控件.在ASP.NET 1.x应用程序中,通过对数据控件的DataSource属性赋值然后调用控件的DataBind()方法从而实现数据展现的方式在ASP.NET 2.0应用程序中可以继续使用.然而,ASP.NET 2.0的数据源控件提供了一种新的方式,使用这些控件可以让你轻松从上文中创建的业务逻辑层中进行数据绑定,甚

  • asp.net中的GridView分页问题

    本文实例讲述了asp.net中的GridView分页问题.分享给大家供大家参考.具体分析如下: 在ASP.NET中,经常会使用到GridView的分页,一般情况下,若使用Visual Studio自带的数据源控件,不会出现什么问题. 但是如果自己用代码设置GridView的数据源,GridView中第一页显示正常,这时如果点击第二页,则经常会出现如下错误 提示:GridView"GridView_test"激发了未处理的事件"PageIndexChanging".

随机推荐