重绘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;
重绘DataGridView的DataGridViewCheckBoxCell控件的更多相关文章
- 玩转控件:重绘DEVEXPRESS中DateEdit控件 —— 让DateEdit支持只选择年月 (提供源码下载)
前言 上一篇博文<玩转控件:重绘ComboBox —— 让ComboBox多列显示>中,根据大家的回馈,ComboBox已经支持筛选了,更新见博文最后最后最后面. 奇葩 这两天遇到 ...
- C# DataGridView自定义分页控件
好些日子不仔细写C#代码了,现在主要是Java项目,C#.Net相关项目不多了,有点手生了,以下代码不足之处望各位提出建议和批评. 近日闲来无事想研究一下自定义控件,虽然之前也看过,那也仅限于皮毛,粗 ...
- 使用DataGridView数据窗口控件,构建用户快速输入体验
在"随风飘散" 博客里面,介绍了一个不错的DataGridView数据窗口控件<DataGridView数据窗口控件开发方法及其源码提供下载>,这种控件在有些场合下,还 ...
- C# 自定义重绘DataGridView
using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using Syste ...
- Winform带dataGridview的Combox控件
调用控件: public partial class Form1 : Form { public Form1() { InitializeComponent(); //---------------- ...
- 初识DataGridView 表格数据控件
DataGridView控件提供了一种强大而灵活的以表格形式显示数据的方式,用户可以使用DataGridView控件来显示少量数据的只读视图,也可以对其进行缩放以显示特大数据集的可编辑视图. 扩展Da ...
- 重绘DataGridView标头
最近突然想在DataGridView标头放置一个CheckBox,我就想着重写下DataGridViewColumnHeaderCell抱着试试的心态结果真的是可以的下面是源码:(如果有看不懂的可以加 ...
- winfrom中DataGridView绑定数据控件中DataGridViewCheckBoxColumn怎么选中
; i < this.dataGridView1.Rows.Count; i++) { this.dataGridView1.Rows[i].Cells["CheckBoxCulums ...
- 自绘CProgressCtrl进度条控件,支持自定义显示文本和进程百分比信息
// CXProgressCtrl 头文件 #pragma once // CXProgressCtrl class CXProgressCtrl : public CProgressCtrl { D ...
随机推荐
- Ubuntu下使用Sysvinit实现自定义服务(简单研究)
通过上一篇文章http://www.cnblogs.com/EasonJim/p/7168216.html可以大概了解到Sysvinit的历史. 其实在自定义服务上,使用Sysvinit是最简单的,本 ...
- NetCore发布WebApi项目到IIS服务器中
1.确保已在机器上安装Net Core Runtime,,下载地址: https://dotnet.microsoft.com/download 2.点击WebApi项目右键->发布,选择IIS ...
- 安卓下载文件怎样更新UI进度
曾经写过几篇关于下载的文章.总的来说是下面几点: 1.维护一个下载进程的Hashmap,key:使用Md5进行处理后的文件下载地址,value为下载的Task. 以防止下载反复.并将信息保存至数据库. ...
- Windows编程中char*转LPCWSTR解决的方法总结
Windows编程中常常涉及到的一个问题是字符串之间的转换,开发过程总是遇到编译器提示无法格式转换的问题.于是自己总结了几种解决的方法. 1.通过T2W转换宏 char* szStr = " ...
- uva 10716 Evil Straw Warts Live(贪心回文串)
这道题目我用了一上午才做出来,还是看的别人的思路,尽管没有看代码做的有点慢.代码能力还是得加强啊.思维 得缜密.不能想当然,要有根据,写上的代码要有精确度.省的以后还得慢慢调试 思路:贪心.每次都查看 ...
- I/O虚拟化
note:这里主要记录我对IO虚拟化的理解,希望这篇文章对想了解虚拟化IO的同学有点帮助.这是我在看论文[vale,a switched ethernet for virtual machines]的 ...
- Python3基础(五) 函数
函数(function)是组织好的.可重复使用的.具有一定功能的代码段.函数能提高应用的模块性和代码的重复利用率,Python中已经提供了很多内建函数,比如print(),同时Python还允许用户自 ...
- ios 使用第三方框架注意
在ios中使用第三方类库 在项目开发中经常会用到一些第三方类库,通常有两种方法来做到:一种方法是直接把所有的.h和.m文件复制到项目中:另一种方法是把.xcodeproj拖到项目中生成静 ...
- 【转】 vsftp上传文件出现553 Could not create file解决方法
因工作需要,需要搭建一个ftp服务器,我使用ubuntu 10.04操作系统,下载vsftpdy源代码, 进行了编译,安装,然后按照INSTALL文件,创建了用户等操作. 因为时间比较紧,我采用 ...
- LeetCode 246. Strobogrammatic Number (可颠倒数字) $
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...