栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > NET面试题库

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

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

这个例子实现了一个把数据中的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改成以上的这个就可以了

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/266305.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号