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

重绘DataGridView的DataGridViewCheckBoxCell控件的更多相关文章

  1. 玩转控件:重绘DEVEXPRESS中DateEdit控件 —— 让DateEdit支持只选择年月 (提供源码下载)

      前言 上一篇博文<玩转控件:重绘ComboBox —— 让ComboBox多列显示>中,根据大家的回馈,ComboBox已经支持筛选了,更新见博文最后最后最后面.   奇葩 这两天遇到 ...

  2. C# DataGridView自定义分页控件

    好些日子不仔细写C#代码了,现在主要是Java项目,C#.Net相关项目不多了,有点手生了,以下代码不足之处望各位提出建议和批评. 近日闲来无事想研究一下自定义控件,虽然之前也看过,那也仅限于皮毛,粗 ...

  3. 使用DataGridView数据窗口控件,构建用户快速输入体验

    在"随风飘散" 博客里面,介绍了一个不错的DataGridView数据窗口控件<DataGridView数据窗口控件开发方法及其源码提供下载>,这种控件在有些场合下,还 ...

  4. C# 自定义重绘DataGridView

    using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using Syste ...

  5. Winform带dataGridview的Combox控件

    调用控件: public partial class Form1 : Form { public Form1() { InitializeComponent(); //---------------- ...

  6. 初识DataGridView 表格数据控件

    DataGridView控件提供了一种强大而灵活的以表格形式显示数据的方式,用户可以使用DataGridView控件来显示少量数据的只读视图,也可以对其进行缩放以显示特大数据集的可编辑视图. 扩展Da ...

  7. 重绘DataGridView标头

    最近突然想在DataGridView标头放置一个CheckBox,我就想着重写下DataGridViewColumnHeaderCell抱着试试的心态结果真的是可以的下面是源码:(如果有看不懂的可以加 ...

  8. winfrom中DataGridView绑定数据控件中DataGridViewCheckBoxColumn怎么选中

    ; i < this.dataGridView1.Rows.Count; i++) { this.dataGridView1.Rows[i].Cells["CheckBoxCulums ...

  9. 自绘CProgressCtrl进度条控件,支持自定义显示文本和进程百分比信息

    // CXProgressCtrl 头文件 #pragma once // CXProgressCtrl class CXProgressCtrl : public CProgressCtrl { D ...

随机推荐

  1. pt工具加字段脚本

    #!/bin/bashcnn_db=$1table=$2alter_conment=$3 cnn_host='192.168.10.14'cnn_user='root'cnn_pwd='123456' ...

  2. Ubuntu 16.04开机自动开启数字键盘NumLock

    注意:以下方法不适合在登录时开启,只能是登录后开启.而且我经过测试之后会自动暗下去,但是不影响功能使用.这个是BUG的修复方法:https://askubuntu.com/questions/5090 ...

  3. bridge 上网

    手头有一台机器上有一个虚拟机 虚拟机为桥接 本机为静态IP上网 想让虚拟机上网的方式是 本机不设ip 网卡设置DHCP. 虚拟机设置静态ip

  4. 递归删除目录下.svn文件

    原文:http://imysqldba.blog.51cto.com/1222376/1104901 ------------------------------------------------- ...

  5. Solidworks如果有两个相似的图纸如何快速复制第二份图纸

    如下图所示,我有两个零件,只有四个孔从螺纹孔改成了通孔(孔的尺寸改大了一点) 我已经画好了带螺纹的图纸   直接另存为,但是不要勾选另存为副本,改一下另存为的名字即可   然后打开这个另存为的工程图, ...

  6. 解决多个Xcode导致的N个模拟器的问题

    <欢迎大家增加iOS开发学习交流群:QQ529560119> 完美解决多个Xcode从而导致了出现N个模拟器的问题

  7. mac 显示隐藏文件的命令行和快捷键

    命令行方式: 显示隐藏文件: defaults write com.apple.Finder AppleShowAllFiles YES;KillAll Finder 不显示隐藏文件: default ...

  8. [DB][MySql]关于取得自增字段的值、及@@IDENTITY 与并发性问题

    对于刚从Oracle转向MySql的人都会为,MySql中没有Oracle里的Sequence而感到困惑.MySql中没有了Sequence,那么MySql的主键用什么方式来实现最好呢? 主要有下面几 ...

  9. Hadoop之父Doug Cutting:Lucene到Hadoop的开源之路

    Hadoop之父Doug Cutting:Lucene到Hadoop的开源之路 Doug Cutting,凭借自己对工作的热情和脚踏实地的态度,开创了Lucene和Nutch两个成功的开源搜索引擎项目 ...

  10. Java —— 正则表达式

    0. 注意 正则表达式里的点号(.),可以匹配除换行符之外的所有字符 Java 语言同其他语言中的正则表达式的不同在于: 对 \(反斜线)的不同处理 \\:其他语言中,表示在正则表达式中插入普通的反斜 ...