最近突然想在DataGridView标头放置一个CheckBox,我就想着重写下DataGridViewColumnHeaderCell
抱着试试的心态结果真的是可以的
下面是源码:(如果有看不懂的可以加 源码分享群 81582487 来问我)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Windows.Forms.VisualStyles;
namespace WindowsDemo
{
public class MyDataGridViewColumnHeaderCell : DataGridViewColumnHeaderCell
{
public delegate void CheckBoxClick(DataGridViewCellMouseEventArgs e);
public delegate void ButtonClick(DataGridViewCellMouseEventArgs e);
public event CheckBoxClick checkboxClick = null;
public event ButtonClick buttonClick = null;
public MyDataGridViewColumnHeaderCell()
: base()
{
}
private Rectangle ChecboxLocation
{
get;
set;
}
private Rectangle ButtonLocation { get; set; }
public CheckBoxState CheckBoxTestState { get; set; }
/// <summary>
/// 单元格边框颜色
/// </summary>
private Color CellBorderColor { get { return Color.FromArgb(172, 168, 153); } }
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle
cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value,
object formattedValue, string errorText, DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
var check = (Boolean)value;
CheckBoxTestState = check ? CheckBoxState.CheckedNormal :
CheckBoxState.UncheckedNormal;
if (paintParts == DataGridViewPaintParts.Background || paintParts ==
DataGridViewPaintParts.All)
{
graphics.FillRectangle(new SolidBrush(cellStyle.BackColor), cellBounds);
}
if (paintParts == DataGridViewPaintParts.Border || paintParts ==
DataGridViewPaintParts.All)
{
graphics.DrawRectangle(new Pen(CellBorderColor), cellBounds);
}
if (paintParts == DataGridViewPaintParts.SelectionBackground || Selected)
{
graphics.FillRectangle(new SolidBrush(cellStyle.SelectionBackColor),
cellBounds);
}
Point checboxPoint = new Point(cellBounds.X + 4, cellBounds.Y + cellBounds.Height
/ 4);
Rectangle checboxRegtangle = new Rectangle(new Point(cellBounds.X + 4,
cellBounds.Y + cellBounds.Height / 4), new Size(40, 40));
Rectangle buttonRectangle = new Rectangle(new Point(cellBounds.X + 50,
cellBounds.Y + cellBounds.Height / 4), new Size(40, 20));
ChecboxLocation = checboxRegtangle;
ButtonLocation = buttonRectangle;
CheckBoxRenderer.DrawCheckBox(graphics, checboxPoint,checboxRegtangle,"tasi",new
Font("宋体",12),false,CheckBoxTestState);
ButtonRenderer.DrawButton(graphics, buttonRectangle, true,
PushButtonState.Default);
// base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value,
formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
}
//DataGridview标头单元格绘制两个按钮,并指定两个按钮的事件
protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
{
//这里位置要计算好,计算出CheckBox位置
if (e.Location.X > ChecboxLocation.Location.X && e.Location.X <
ChecboxLocation.Location.X + ChecboxLocation.Width)
{
if (checkboxClick != null)
{
checkboxClick(e);
}
}
//计算出button的位置
else if(e.Location.X
>ButtonLocation.Location.X&&e.Location.X<ButtonLocation.Location.X + ButtonLocation.Width)
{
buttonClick(e);
}
base.OnMouseClick(e);
}
}
}
使用也很简单:(使用时先把结构加载完了在对这列进行如下操作)
MyDataGridViewColumnHeaderCell mycell = new MyDataGridViewColumnHeaderCell();
mycell.checkboxClick+=new MyDataGridViewColumnHeaderCell.CheckBoxClick
(mycell_checkboxClick);
mycell.buttonClick += new MyDataGridViewColumnHeaderCell.ButtonClick
(mycell_buttonClick);
mycell.Value = true;
this.view.Columns[0].HeaderCell = mycell;
view.CellPainting += new DataGridViewCellPaintingEventHandler
(dataGridView1_CellPainting);
这个里面我写了两个控件,有点注意的是点击事件时我对按钮的位置没有控制好,我暂时没有认真写这个,
要是用的话自己要想下如何控制点击事件的位置,第二个是按钮的大小我没有控制,这个也需要自己来控制