最近项目中要用到在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;

最新文章

  1. html热力图的操作(点击图片的不同位置操作不同的事件)适合说明文档
  2. LINUX下搭建VPN
  3. 移除\禁用 jquery mobile 元素样式渲染
  4. 供应商 银行 SQL (转自ITPUB)
  5. 让jquery.tmpl.js支持index序号
  6. js字符串大小写转换
  7. FreeCodecamp:Repeat a string repeat a string
  8. poj1804(归并排序求逆序数)
  9. codevs3027线段覆盖2(DP)题解
  10. DDD理论学习系列(9)-- 领域事件
  11. 定时任务之crontab命令
  12. 关于php加密库加密数据上传数据库或解密出错的问题
  13. Mac OS X版本的sublime text 3安装汇编语言语法支持
  14. keystonejs开发中解决bug--版本要对应
  15. java操作JacocClient下载dump文件
  16. ServletContext、ServletRequest和HttpSession的生命周期
  17. python的Web框架,html分页
  18. 使用matlab自带工具实现rcnn
  19. PHP之ctype扩展
  20. ECMAScript6新特性之Array API

热门文章

  1. PHP与WCF第一次亲密接触
  2. mybatis表关联彻底理解
  3. linux下让irb实现代码自己主动补全的功能
  4. ubuntu Change Language
  5. uva 11212 - Editing a Book(迭代加深搜索 IDA*) 迭代加深搜索
  6. JAVA学习(三):Java基础语法(变量、常量、数据类型、运算符与数据类型转换)
  7. expect安装测试-批量用户管理
  8. apache ua Custom Log Formats
  9. HDU 1299Diophantus of Alexandria
  10. Python三次登陆