日期:2014-05-19  浏览次数:21545 次

求助: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;
}
}