重绘DataGridView的DataGridViewCheckBoxCell控件
最近项目中要用到在DataGridView单元格里面放置一个带有文本的 DataGridViewCheckBoxCell控件但原有
的是不支持的然后我就想着重写个 DataGridViewCheckBoxTextCell
下面是源码:(如有疑问可以加源码分享群 81582487 来问我)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Drawing;
namespace WindowsDemo
{
public class DataGridViewCheckBoxTestCell : DataGridViewCheckBoxCell
{
public DataGridViewCheckBoxTestCell()
: base()
{
}
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 cellState, 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);
}
var col = OwningColumn as DataGridViewCheckBoxTextColumn;
if (col != null && !string.IsNullOrEmpty(col.Text))
{
graphics.DrawString(col.Text, cellStyle.Font, new SolidBrush(Selected ?
cellStyle.SelectionForeColor : cellStyle.ForeColor),
new Point(cellBounds.X + 25, cellBounds.Y + cellBounds.Height / 4));
}
if (!string.IsNullOrEmpty(Text))
{
graphics.DrawString(Text, cellStyle.Font, new SolidBrush(Selected ?
cellStyle.SelectionForeColor : cellStyle.ForeColor),
new Point(cellBounds.X + 25, cellBounds.Y + cellBounds.Height / 4));
}
CheckBoxRenderer.DrawCheckBox(graphics, new Point(cellBounds.X + 4, cellBounds.Y +
cellBounds.Height / 4), CheckBoxTestState);
// ButtonRenderer.DrawButton(graphics, new Rectangle(new Point(cellBounds.X + 50,
cellBounds.Y + cellBounds.Height / 4), new Size(20, 20)), true, PushButtonState.Default);
// base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value,
formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
}
protected override void OnMouseDown(DataGridViewCellMouseEventArgs e)
{
var check = (bool)Value;
CheckBoxTestState = check ? CheckBoxState.CheckedNormal :
CheckBoxState.UncheckedNormal;
Value = !check;
base.OnMouseDown(e);
}
public string Text { get; set; }
}
}
使用也很简单:(使用时先把结构加载完了在对这列进行如下操作)
DataGridViewCheckBoxTestCell check3 = new DataGridViewCheckBoxTestCell();
check3.Text = "请选择";
check3.Value = true;
row.Cells["check"] = check3;
最新文章
- html热力图的操作(点击图片的不同位置操作不同的事件)适合说明文档
- LINUX下搭建VPN
- 移除\禁用 jquery mobile 元素样式渲染
- 供应商 银行 SQL (转自ITPUB)
- 让jquery.tmpl.js支持index序号
- js字符串大小写转换
- FreeCodecamp:Repeat a string repeat a string
- poj1804(归并排序求逆序数)
- codevs3027线段覆盖2(DP)题解
- DDD理论学习系列(9)-- 领域事件
- 定时任务之crontab命令
- 关于php加密库加密数据上传数据库或解密出错的问题
- Mac OS X版本的sublime text 3安装汇编语言语法支持
- keystonejs开发中解决bug--版本要对应
- java操作JacocClient下载dump文件
- ServletContext、ServletRequest和HttpSession的生命周期
- python的Web框架,html分页
- 使用matlab自带工具实现rcnn
- PHP之ctype扩展
- ECMAScript6新特性之Array API