C# WinForm下DataGridView单选按钮列和支持三种选择状态的复选框列的实现
来源:互联网 发布:java 动态添加字段 编辑:程序博客网 时间:2024/05/22 03:49
上面两个截图分别为RadioButton列和支持三种状态的CheckBox列在DataGridView中的实现效果,我是在Windows 2003中实现的,因此显示的效果跟在XP和Vista下有些区别,Vista下CheckBox的第三种状态(不确定状态)显示出来的效果是一个实心的蓝色方块。
下面我看具体来看看如何实现这两种效果。
要实现自定义的DataGridView列,你需要继承并改写两个类,一个是基于DataGridViewColumn的,一个是基于DataGridViewCell的,因为RadionButton和CheckBox的实现原理类似,因此我们可以将这两种列采用同一种方法实现。创建DataGridViewDisableCheckBoxCell和DataGridViewDisableCheckBoxColumn两个类,分别继承自DataGridViewCheckBoxCell和DataGridViewCheckBoxColumn。代码如下:
public class DataGridViewDisableCheckBoxCell: DataGridViewCheckBoxCell
{
public bool Enabled { get; set; }
// Override the Clone method so that the Enabled property is copied.
public override object Clone()
{
DataGridViewDisableCheckBoxCell cell = (DataGridViewDisableCheckBoxCell)base.Clone();
cell.Enabled = this.Enabled;
return cell;
}
// By default, enable the CheckBox cell.
public DataGridViewDisableCheckBoxCell()
{
this.Enabled = true;
}
// Three state checkbox column cell
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
DataGridViewElementStates elementState, object value, object formattedValue, string errorText,
DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
// The checkBox cell is disabled, so paint the border, background, and disabled checkBox for the cell.
if (!this.Enabled)
{
// Draw the cell background, if specified.
if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
{
SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor);
graphics.FillRectangle(cellBackground, cellBounds);
cellBackground.Dispose();
}
// Draw the cell borders, if specified.
if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)
{
PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
}
// Calculate the area in which to draw the checkBox.
CheckBoxState state = CheckBoxState.MixedDisabled;
Size size = CheckBoxRenderer.GetGlyphSize(graphics, state);
Point center = new Point(cellBounds.X, cellBounds.Y);
center.X += (cellBounds.Width - size.Width) / 2;
center.Y += (cellBounds.Height - size.Height) / 2;
// Draw the disabled checkBox.
CheckBoxRenderer.DrawCheckBox(graphics, center, state);
}
else
{
// The checkBox cell is enabled, so let the base class, handle the painting.
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
}
}
}
public class DataGridViewDisableCheckBoxColumn : DataGridViewCheckBoxColumn
{
public DataGridViewDisableCheckBoxColumn()
{
this.CellTemplate = new DataGridViewDisableCheckBoxCell();
}
}
- C# WinForm下DataGridView单选按钮列和支持三种选择状态的复选框列的实现
- C# WinForm下DataGridView单选按钮列和支持三种选择状态的复选框列的实现
- DataGridView单元按钮列和支持三种选择状态的复选框列的实现
- C# WinForm下DataGridView单元按钮列(一)
- C# WinForm下DataGridView单元按钮列(二)
- datagridview的按钮列
- datagridview的按钮列
- winform中datagrid和datagridview设置列标题的实现
- C#给WinForm(窗体)下DataGridView控件添加单选选或多选按钮列
- c# datagridview列头添加复选框实现全选
- 如何设计WinForm中DataGridView控件的自定义按钮列
- WinForm 处理 DataGridView 按钮列
- c# winform 用代码修改DataGridView列头的名字,设置列名,修改列名
- C# winform关于datagridview中的列的数据类型转换问题
- C#实现WinForm下DataGridView控件的拷贝和粘贴
- winform中的datagridview里添加按钮列,如何响应按钮的点击事件
- winform中的datagridview有添加按钮列,响应按钮的点击事件
- WinForm中DataGridView列的顺序
- STL中vector容器中元素为自定义类型时的效率分析
- DDTCMS --Django Dream Team Content Management System
- 用eclipse导出jar文件时加入第三方jar包的支持
- Django 处理 unicode的方法
- Storage duration(C++学习)
- C# WinForm下DataGridView单选按钮列和支持三种选择状态的复选框列的实现
- Django:DDTCMS编码规划
- 有点意思的C/C++问题及解答:11-15
- Django:DDTCMS新闻及图片处理方案
- 阿里软件接口开发基础(淘宝网) C#
- Django:DDTCMS新闻系统规划设计与实验
- du磁盘空间
- C#制作的GMail的邮箱阅读器
- Django:DDTCMS关于皮肤Themes的想法