首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

DataGridView添加列时CellType属性为空的错误

2012-01-19 
求助:DataGridView添加列时CellType属性为空的异常根据网上片贴子http://paulstovell.net/blog/index.php/

求助:DataGridView添加列时CellType属性为空的异常
根据网上片贴子   http://paulstovell.net/blog/index.php/custom-drawn-datagridview-cells-with-gdi/
想在DataGridVIew中显示一个百分比的图片

ProgressColumn   是继承自   DataGridViewColumn

不通过设计器   在程序中添加如下代码

ProgressColumn   pc   =   new   ProgressColumn();
pc.Name   =   "percent ";
pc.HeaderText   =   "百分比 ";
dataGridView1.Columns.Add(pc);

运行到最后一行出错,提示:无法添加该列,原因是它的   CellType   属性为空
请问如何设置?谢谢!

[解决办法]
设置他的CellType 属性
[解决办法]
public override DataGridViewCell CellTemplate
你要重写这个属性。
如:(这是从DataGridViewTextBoxColumn类取出来的)
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
if ((value != null) && !(value is DataGridViewTextBoxCell))
{
throw new InvalidCastException(SR.GetString( "DataGridViewTypeColumn_WrongCellTemplateType ", new object[] { "System.Windows.Forms.DataGridViewTextBoxCell " }));
}
base.CellTemplate = value;
}
}

热点排行