C#重寫DataGridView
更新時(shí)間:2016年05月04日 16:09:52 投稿:lijiao
這篇文章主要為大家詳細(xì)介紹了C#重寫DataGridView的相關(guān)資料,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C#重寫DataGridView的實(shí)例代碼,供大家參考,具體內(nèi)容如下
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;//表格點(diǎn)擊后顏色 表格線顏色
this.BackgroundColor = System.Drawing.SystemColors.Window;
this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.AllowUserToOrderColumns = true;
this.AutoGenerateColumns = true;
base.OnCreateControl();
}
Color defaultcolor;
//移到單元格時(shí)的顏色
protected override void OnCellMouseMove(DataGridViewCellMouseEventArgs e)
{
base.OnCellMouseMove(e);
try
{
if (_CellColorOnchange)
Rows[e.RowIndex].DefaultCellStyle.BackColor = cell_color;
}
catch (Exception)
{
}
}
//進(jìn)入單元格時(shí)保存當(dāng)前的顏色
protected override void OnCellMouseEnter(DataGridViewCellEventArgs e)
{
base.OnCellMouseEnter(e);
try
{
if (_CellColorOnchange)
defaultcolor = Rows[e.RowIndex].DefaultCellStyle.BackColor;
}
catch (Exception)
{
}
}
//離開時(shí)還原顏色
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.未標(biāo)題_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.未標(biāo)題_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)
{
//標(biāo)題行
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)
{
//標(biāo)題列
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);
}
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
c#判斷網(wǎng)絡(luò)連接狀態(tài)的示例分享
這篇文章主要介紹了使用c#判斷網(wǎng)絡(luò)連接狀態(tài)的示例,需要的朋友可以參考下2014-02-02
C#使用控制臺(tái)列出當(dāng)前所有可用的打印機(jī)列表
這篇文章主要介紹了C#使用控制臺(tái)列出當(dāng)前所有可用的打印機(jī)列表,涉及C#操作計(jì)算機(jī)硬件的相關(guān)使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
Winform中實(shí)現(xiàn)圖片格式轉(zhuǎn)換
這篇文章主要介紹了Winform中實(shí)現(xiàn)圖片格式轉(zhuǎn)換的示例代碼,幫助大家更好的理解和使用winform開發(fā),感興趣的朋友可以了解下2020-12-12
WinForm實(shí)現(xiàn)讀取Resource中文件的方法
這篇文章主要介紹了WinForm實(shí)現(xiàn)讀取Resource中文件的方法,很實(shí)用的一個(gè)功能,需要的朋友可以參考下2014-08-08
C#將html table 導(dǎo)出成excel實(shí)例
C#將html table 導(dǎo)出成excel實(shí)例,需要的朋友可以參考一下2013-04-04
C# listview添加combobox到單元格的實(shí)現(xiàn)代碼
從別處轉(zhuǎn)來的,自己進(jìn)行了一些小的修改,還不錯(cuò),你自己先拖一個(gè)ListView1和一個(gè)ComboBox1,需要的朋友可以參考下2014-06-06

