最近项目中要用到在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. windows下如何正确使用Jconsole远程连接linux主机上的JVM

    https://www.aliyun.com/jiaocheng/589230.html

  2. 洛谷——P1082 同余方程

    P1082 同余方程 题目描述 求关于 x 的同余方程 ax ≡ 1 (mod b)的最小正整数解. 输入输出格式 输入格式: 输入只有一行,包含两个正整数 a, b,用一个空格隔开. 输出格式: 输 ...

  3. Java使用JNI调用DLL库

    JNI是Java自带的方法,不需要引入第三方jar包,优点是因为是java自带的方法,兼容性较好,缺点就是代码书写繁琐 新建Java项目Test --> 新建测试类TestNative,声明本地 ...

  4. - > 最大公约数(辗转相除法)和最小公倍数(公式法)

    最大公约数 #include<iostream> using namespace std; int a,b; int gcd(int x,int y){ return x==0?y:gcd ...

  5. Angularjs中添加HighCharts

    一. 添加基本配置 1. 添加指令 angular.module('newApp') .directive('dpHighchart', ['$rootScope', function($rootSc ...

  6. PHP array_merge()

    定义和用法 array_merge() 函数把两个或多个数组合并为一个数组. 如果键名有重复,该键的键值为最后一个键名对应的值(后面的覆盖前面的).如果数组是数字索引的,则键名会以连续方式重新索引. ...

  7. [Vue @Component] Write Vue Functional Components Inline

    Vue's functional components are small and flexible enough to be declared inside of .vue file next to ...

  8. Windows 9立即公布了

    Windows 9技术预览版可能于今晚在美国旧金山举办的Windows公布会上宣布.下一代的Windows名称最有可能是Windows或Windows 9.网友猜想也可能是WIndows X或Wind ...

  9. java梳理-反射

    本文属于面试题梳理系列:问题:java反射类的訪问私有方法与普通方法相比,须要多处理什么?  之前梳理类载入的时候,介绍到初始化的时机之中的一个:用java.lang.reflect包的方法对类进行反 ...

  10. Levenberg–Marquardt algorithm

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdGFubWVuZ3dlbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...