如何为DataGridView添加一个定制的Column Type


Posted in 面试题 onJanuary 21, 2014
这个例子实现了一个把数据中的Boolean值用Y或者N在DataGridView里面显示,步骤如下:

1. 建立一个继承DataGridViewTextBoxCell的类, 代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace com.Threes.CustomControl
{
public class DataGridViewBooleanCell : DataGridViewTextBoxCell
{
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)
{
// Call the base class method to paint the default cell appearance.
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,
value, “”, errorText, cellStyle,
advancedBorderStyle, paintParts);
if (value is Boolean && (bool)value == true)
{
graphics.DrawString(“Y”, cellStyle.Font, new SolidBrush(cellStyle.ForeColor), cellBounds.X, cellBounds.Y);
}

}

}

}

2. 建立一个继承自DataGridViewColumn的类 代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace com.Threes.CustomControl
{
public class DataGridViewBooleanColumn : DataGridViewColumn
{
public DataGridViewBooleanColumn()
{
this.CellTemplate = new DataGridViewBooleanCell();
}
}
}

然后把你的DataGridView里面的Boolean列的ColumnType改成以上的这个就可以了

Tags in this post...

面试题 相关文章推荐
error和exception有什么区别
Oct 02 面试题
C语言笔试题
Sep 04 面试题
我能否用void** 指针作为参数, 使函数按引用接受一般指针
Feb 16 面试题
求∏的近似值,直到最后一项的绝对值小于指定的数
Feb 12 面试题
System.Array.CopyTo()和System.Array.Clone()有什么区别
Jun 20 面试题
What's the difference between an interface and abstract class? (接口与抽象类有什么区别)
Oct 29 面试题
Linux如何为某个操作添加别名
Mar 01 面试题
Linux内核产生并发的原因
Jul 13 面试题
Linux如何为某个操作添加别名
Feb 05 面试题
what is the difference between ext2 and ext3
Nov 03 面试题
什么是封装
Mar 26 面试题
Java servlet面试题
Mar 04 面试题
Net Remoting把服务器端激活两种模式
Jan 22 #面试题
.net软件工程师应聘上机试题
Mar 10 #面试题
请解释在new与override的区别
Oct 29 #面试题
关于递归的一道.NET面试题
May 12 #面试题
杭州信雅达系统.NET工程师面试试题
Feb 08 #面试题
请解释一下webService? 如何用.net实现webService
Jun 09 #面试题
请编程遍历页面上所有 TextBox 控件并给它赋值为 string.Empty
Dec 03 #面试题
You might like
php 定义404页面的实现代码
2012/11/19 PHP
php判断linux下程序问题实例
2015/07/09 PHP
PHP 超级全局变量相关总结
2020/06/30 PHP
setTimeout与setInterval在不同浏览器下的差异
2010/01/24 Javascript
js下判断 iframe 是否加载完成的完美方法
2010/10/26 Javascript
jquery动态加载select下拉框示例代码
2013/12/10 Javascript
JavaScript控制图片加载完成后调用回调函数的方法
2015/03/20 Javascript
JS组件Bootstrap Table表格行拖拽效果实现代码
2020/08/27 Javascript
AngularJS中的$watch(),$digest()和$apply()区分
2016/04/04 Javascript
简单的JS轮播图代码
2016/07/18 Javascript
JS简单实现表格排序功能示例
2016/12/20 Javascript
浅谈struts1 & jquery form 文件异步上传
2017/05/25 jQuery
Angular2监听页面大小变化的解决方法
2017/10/09 Javascript
微信小程序组件之srcoll-view的详解
2017/10/19 Javascript
Node.js readline 逐行读取、写入文件内容的示例
2018/03/01 Javascript
React BootStrap用户体验框架快速上手
2018/03/06 Javascript
原生js实现自定义消息提示框
2020/11/19 Javascript
Python对象的深拷贝和浅拷贝详解
2014/08/25 Python
Python中的命令行参数解析工具之docopt详解
2017/03/27 Python
Python3 JSON编码解码方法详解
2019/09/06 Python
tensorflow没有output结点,存储成pb文件的例子
2020/01/04 Python
Pytorch 实现计算分类器准确率(总分类及子分类)
2020/01/18 Python
Python中使用socks5设置全局代理的方法示例
2020/04/15 Python
详解用selenium来下载小姐姐图片并保存
2021/01/26 Python
英国安全产品购物网站:The Safe Shop
2017/03/20 全球购物
迪卡侬英国官网:Decathlon英国
2017/04/08 全球购物
在线学习西班牙语、法语或其他语言:Babbel.com
2018/02/07 全球购物
JSF面试题:Jsf中导航的标签是什么
2013/04/20 面试题
中秋节礼品促销方案
2014/02/02 职场文书
2015年科协工作总结
2015/05/19 职场文书
2015年卫生监督工作总结
2015/05/21 职场文书
2019奶茶店创业计划书范本!
2019/07/15 职场文书
《曾国藩家书》读后感——读家书,立家风
2019/08/21 职场文书
MySQL中你可能忽略的COLLATION实例详解
2021/05/12 MySQL
Python制作春联的示例代码
2022/01/22 Python
人物搭配车车超萌联名预备中 【咒术迴战】 ⨯ 【天竺鼠车车】 展开合作
2022/04/11 日漫