国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#重寫DataGridView

2020-01-24 01:10:32
字體:
來源:轉載
供稿:網友

 本文實例為大家分享了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);      }    }  }}

以上就是本文的全部內容,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 盐城市| 华安县| 磐石市| 密山市| 合山市| 达孜县| 宜黄县| 无极县| 来凤县| 南昌县| 元朗区| 金坛市| 米林县| 彰武县| 钦州市| 新丰县| 海口市| 兴安县| 高雄市| 宝丰县| 锡林郭勒盟| 会同县| 赤峰市| 长顺县| 广元市| 肇东市| 施秉县| 翁牛特旗| 老河口市| 平邑县| 红安县| 普定县| 阳西县| 陆河县| 集贤县| 岢岚县| 乐平市| 高阳县| 高阳县| 兴安盟| 山西省|