设为首页收藏本站

嘻皮客娱乐学习网

 找回密码
 中文注册
搜索
打印 上一主题 下一主题
开启左侧

[面试题库] 如何为DataGridView添加一个定制的Column Type

[复制链接]
跳转到指定楼层
楼主
发表于 2014-11-30 13:56:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
这个例子实现了一个把数据中的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改成以上的这个就可以了
回复

使用道具 举报

小黑屋|手机版|嘻皮客网 ( 京ICP备10218169号|京公网安备11010802013797  

GMT+8, 2024-4-25 10:02 , Processed in 0.188597 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表