.Net开发中我们经常要自定义DataGrid列以满足我们的开发需要,网上常见的是TextBox,ProgressBar等,对布尔值则只有标准的DataGridBoolColumn提供,但因为其内部的很多属性封装,导致无法使用。
因此我们基于DataGrid列的模式,实现了CheckBox列,代码如下:
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
namespace Staffer
{
/// <summary>
/// DataGridCheckBoxColumn 的摘要说明。
/// </summary>
public class DataGridCheckBoxColumn : DataGridColumnStyle
{
private CheckBox myCheckBox = new CheckBox();
private bool isEditing;
public DataGridCheckBoxColumn() : base()
{
this.Alignment = HorizontalAlignment.Center;
myCheckBox.Checked = false;
myCheckBox.Text = "";
myCheckBox.CheckAlign = ContentAlignment.MiddleCenter;
myCheckBox.ThreeState = false;
myCheckBox.Visible = false;
}
public override bool ReadOnly
{
get
{
return base.ReadOnly;
}
set
{
base.ReadOnly = value;
myCheckBox.Enabled = false;
}
}
private void CheckedChanged(object sender, EventArgs e)
{
this.myCheckBox.CheckedChanged -= new EventHandler(CheckedChanged);
this.isEditing = true;
base.ColumnStartedEditing(this.myCheckBox);
}
protected override void Abort(int rowNum)
{
this.isEditing = false;
this.myCheckBox.CheckedChanged -= new EventHandler(CheckedChanged);
//'重新绘制列并向控件发送一条绘制消息
Invalidate();
}
protected override bool Commit(CurrencyManager dataSource, int rowNum)
{
this.myCheckBox.Bounds = Rectangle.Empty;
this.myCheckBox.CheckedChanged -= new EventHandler(CheckedChanged);
if (!this.isEditing) return true;
this.isEditing = false;
bool value = this.myCheckBox.Checked;
//用来自指定 CurrencyManager 的值设置指定行中的值
SetColumnValueAtRow(dataSource, rowNum, value);
//重新绘制列并向控件发送一条绘制消息
Invalidate();
return true;
}
protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
if (this.ReadOnly) return;
bool value = Convert.ToBoolean(GetColumnValueAtRow(source, rowNum));
this.myCheckBox.Checked = value;
//如果单元格是可见的值
if (cellIsVisible)
{
//内嵌控件在单元格之内,小高和宽都小2像素
this.myCheckBox.Bounds = new Rectangle(bounds.X + 2, bounds.Y + 2, bounds.Width - 4, bounds.Height - 4);
//this.myCheckBox.BackColor = DataGridTableStyle.SelectionBackColor;
this.myCheckBox.BackColor = Color.White;
this.myCheckBox.Visible = true;
DataGridTableStyle.DataGrid.Invalidate(bounds);
this.myCheckBox.Focus();
this.myCheckBox.CheckedChanged += new EventHandler(CheckedChanged);
//点击时就改变状态
if (value)
this.myCheckBox.Checked = false;
else
this.myCheckBox.Checked = true;
}
else
{
this.myCheckBox.Visible = false;
}
}
protected override int GetMinimumHeight()
{
return this.myCheckBox.Height;
}
protected override int GetPreferredHeight(Graphics g, object value)
{
return this.myCheckBox.Height;
}
protected override Size GetPreferredSize(Graphics g, object value)
{
return new Size(100, this.myCheckBox.Height);
}
protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum)
{
this.Paint(g, bounds, source, rowNum, false);
}
protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight)
{
this.Paint(g, bounds, source, rowNum, Brushes.Blue, Brushes.Red, alignToRight);
}
protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
{
Rectangle rect = bounds;
g.FillRectangle(backBrush, rect);
bool value = Convert.ToBoolean(GetColumnValueAtRow(source, rowNum));
ButtonState state;
if (value)
{
state = ButtonState.Checked;
}
else
{
state = ButtonState.Normal;
}
int lon = 14;
int x = bounds.Width / 2 - lon / 2 + bounds.X;
int y = bounds.Height / 2 - lon / 2 + bounds.Y;
int width = lon;
int height = lon;
rect = new Rectangle(x, y, width, height);
ControlPaint.DrawCheckBox(g, rect, state);
}
protected override object GetColumnValueAtRow(CurrencyManager source, int rowNum)
{
return base.GetColumnValueAtRow (source, rowNum);
}
protected override void SetColumnValueAtRow(CurrencyManager source, int rowNum, object value)
{
base.SetColumnValueAtRow (source, rowNum, value);
}
protected override void SetDataGridInColumn(DataGrid value)
{
base.SetDataGridInColumn (value);
if (this.myCheckBox.Parent != null)
{
this.myCheckBox.Parent.Controls.Remove
(this.myCheckBox);
}
if (value != null)
{
value.Controls.Add(this.myCheckBox);
}
}
protected override void ConcedeFocus()
{
base.ConcedeFocus();
this.isEditing = false;
}
}
}
下载DataGridCheckboxColumn.cs
|