最近项目中要用到在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. JConsole使用手冊具体解释

    一篇Sun项目主页上介绍JConsole使用的文章,前段时间性能測试的时候大概翻译了一下以便学习,今天整理一下发上来.有些地方也不知道怎么翻,就保留了原文,可能还好理解点.呵呵,水平有限,翻的不好,大 ...

  2. 跳過 Windows RT的UI

    RT启动进入常规桌面 微软Surface RT发布的时间已经不短了,相信很多朋友都已经熟悉了这个全新的平板,并且已经上手.Surface RT开机默认进入的界面为Windows UI,这对于经常使用A ...

  3. VBS调用Windows API函数

    Demon's Blog 忘记了,喜欢一个人的感觉 Demon's Blog  »  程序设计  »  VBS调用Windows API函数 « 用VBS修改Windows用户密码 在VB中创建和使用 ...

  4. 机器学习A

    订阅地址 :  http://blog.csdn.net/lizhe_dashuju/rss/list

  5. 读写Word的组件DocX介绍与入门

    本文为转载内容: 文章原地址:http://www.cnblogs.com/asxinyu/archive/2013/02/22/2921861.html 开源Word读写组件DocX介绍与入门 阅读 ...

  6. Html5 移动游戏开发

    有非常多游戏採用H5技术开发.比方三国来了.巴哈姆特之怒.切绳子等. 我们公司也有多款游戏用H5开发.H5开发成本低.效率高,方便做自己主动更新,可移植性好. 受益于H5技术,我们公司的非常多产品都非 ...

  7. RK平台Android4.4 添加一个新的遥控器支持以及添加特殊按键【转】

    本文转载自:http://blog.csdn.net/coding__madman/article/details/52904063 版权声明:本文为博主原创文章,未经博主允许不得转载. 瑞芯微平台 ...

  8. CSS里面position:relative与position:absolute 区别

    position:absolute这个是绝对定位:是相对于浏览器的定位.比如:position:absolute:left:20px;top:80px; 这个容器始终位于距离浏览器左20px,距离浏览 ...

  9. P2453 [SDOI2006]最短距离 dp

    自己想出来了!这个dp比较简单,而且转移也很简单,很自然,直接上代码就行了. 题干: 一种EDIT字母编辑器,它的功能是可以通过不同的变换操作可以把一个源串X [l..m]变换为新的目标串y[1..n ...

  10. PCB Windows Petya(永恒之蓝)勒索病毒补丁检测代码

    公司内部电脑招受到新的勒索病毒Petya(永恒之蓝)攻击,直接导致受攻击的电脑系统崩贵无法启动,这次勒索病毒攻击影响范围之广,IT,人事,工程,生产,物控等部门都无一幸免,对整个公司运转产生了非常严重 ...