面试啦 - 提供专业面试问题及答案、面试技巧、助您成功面试!
您的当前位置:首页 > 面试题库 > IT面试题 > .net面试题 > 正文

如何为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改成以上的这个就可以了
词条:net面试题
上一篇:请解释流与文件有什么不同 下一篇:委托与事件是什么关系?为什么要使用委托
与该文相关的文章

温馨提示:如果您对面试网有任何建议,请通过网站联系邮箱向我们反馈,感谢各位的建议与支持!