C#重写DataGridView
本文实例为大家分享了C#重写DataGridView的实例代码,供大家参考,具体内容如下
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Drawing;
usingSystem.Windows.Forms;
usingSystem.Drawing.Drawing2D;
namespaceCRD.WinUI.Editors
{
publicclassDataGridView:System.Windows.Forms.DataGridView
{
privatebool_CellColorOnchange=false;
privateColorcell_color=Color.Yellow;
privateboolshifouhuasanjiao=true;
privateColorcolor_grid=Color.FromArgb(236,233,216);
boolclick=false;
publicDataGridView()
{
this.SetStyle(ControlStyles.DoubleBuffer|ControlStyles.AllPaintingInWmPaint,true);
}
protectedoverridevoidOnCreateControl()
{
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();
}
Colordefaultcolor;
//移到单元格时的颜色
protectedoverridevoidOnCellMouseMove(DataGridViewCellMouseEventArgse)
{
base.OnCellMouseMove(e);
try
{
if(_CellColorOnchange)
Rows[e.RowIndex].DefaultCellStyle.BackColor=cell_color;
}
catch(Exception)
{
}
}
//进入单元格时保存当前的颜色
protectedoverridevoidOnCellMouseEnter(DataGridViewCellEventArgse)
{
base.OnCellMouseEnter(e);
try
{
if(_CellColorOnchange)
defaultcolor=Rows[e.RowIndex].DefaultCellStyle.BackColor;
}
catch(Exception)
{
}
}
//离开时还原颜色
protectedoverridevoidOnCellMouseLeave(DataGridViewCellEventArgse)
{
base.OnCellMouseLeave(e);
try
{
if(_CellColorOnchange)
Rows[e.RowIndex].DefaultCellStyle.BackColor=defaultcolor;
}
catch(Exception)
{
}
}
publicboolCellColorOnchange
{
get
{
return_CellColorOnchange;
}
set
{
_CellColorOnchange=value;
}
}
publicColorDefaultcolorSet
{
get
{
returncell_color;
}
set
{
cell_color=value;
}
}
publicboolShifouhua_Sanjiao
{
get
{
returnshifouhuasanjiao;
}
set
{
shifouhuasanjiao=value;
}
}
publicColorContent_Grid_color
{
get
{
returncolor_grid;
}
set
{
color_grid=value;
}
}
privatevoidInitializeComponent()
{
((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
this.SuspendLayout();
//
//DataGridView
//
//this.RowTemplate.Height=17;
((System.ComponentModel.ISupportInitialize)(this)).EndInit();
this.ResumeLayout(false);
}
//RowPostPaint
protectedoverridevoidOnRowPostPaint(DataGridViewRowPostPaintEventArgse)
{
if(shifouhuasanjiao)
{
using(SolidBrushb=newSolidBrush(Color.Black))
{
Imageimage=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);
}
protectedoverridevoidOnRowPrePaint(DataGridViewRowPrePaintEventArgse)
{
if(shifouhuasanjiao)
{
using(SolidBrushb=newSolidBrush(Color.Black))
{
Imageimage=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);
}
protectedoverridevoidOnCellClick(DataGridViewCellEventArgse)
{
if(e.RowIndex>-1&&this.CurrentRow.Index==e.RowIndex)
{
click=true;
}
base.OnCellClick(e);
}
protectedoverridevoidOnCellPainting(DataGridViewCellPaintingEventArgse)
{
base.OnCellPainting(e);
SolidBrushb=newSolidBrush(Color.FromArgb(236,233,216));
PenwhitePen=newPen(color_grid,1);
if(e.ColumnIndex==-1&&e.RowIndex==-1)
{
using(LinearGradientBrushbrush=newLinearGradientBrush(e.CellBounds,Color.Gray,
Color.Gray,LinearGradientMode.ForwardDiagonal))
{
e.Graphics.FillRectangle(b,e.CellBounds);
Rectangleborder=e.CellBounds;
border.Offset(newPoint(-1,-1));
e.Graphics.DrawRectangle(Pens.Gray,border);
}
e.PaintContent(e.CellBounds);
e.Handled=true;
}
elseif(e.RowIndex==-1)
{
//标题行
using(LinearGradientBrushbrush=newLinearGradientBrush(e.CellBounds,Color.Silver,
Color.Silver,LinearGradientMode.Vertical))
{
e.Graphics.FillRectangle(b,e.CellBounds);
Rectangleborder=e.CellBounds;
border.Offset(newPoint(-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;
}
elseif(e.ColumnIndex==-1)
{
//标题列
using(LinearGradientBrushbrush=newLinearGradientBrush(e.CellBounds,Color.Silver,
Color.Silver,LinearGradientMode.Horizontal))
{
e.Graphics.FillRectangle(b,e.CellBounds);
Rectangleborder=e.CellBounds;
border.Offset(newPoint(-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)
Rectangleborder=e.CellBounds;
border.Offset(newPoint(-1,-1));
e.Graphics.DrawRectangle(whitePen,border);
}
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助。