我写了个继承的控件,但是我给他添加属性的时候出问题了,提示两个类型转换无效,代码如下,大家帮忙看下:
提示错误为"AddHeaderInfo[]”的对象无法转换为类型“AddHeaderInfo[]”。
//属性类
public class AddHeaderInfo
{
protected int _ColIndex=0;
public virtual int ColIndex
{
get { return this._ColIndex; }
set
{
if (value < 0)
{
throw new Exception("列索引不能为负~~");
}
else
{
this._ColIndex = value;
}
}
} protected int _ColCount=2;
public virtual int ColCount
{
get { return this._ColCount; }
set
{
if (value < 0)
{
throw new Exception("需要合并的列数量不能为负");
}
else
{
this._ColCount = value;
}
}
}
protected string _HeadText="1212";
public virtual string HeadText
{
get { return this._HeadText; }
set { this._HeadText = value; }
}
}下面是调用类 public partial class CustomControl2 : DataGridView,ISerializable
{
public CustomControl2()
{
InitializeComponent(); this._MergeHead=new List<AddHeaderInfo>();
this.CellPainting += new DataGridViewCellPaintingEventHandler(CustomControl2_CellPainting);
this.Scroll += new ScrollEventHandler(CustomControl2_Scroll); }
protected List<AddHeaderInfo> _MergeHead;
public virtual List<AddHeaderInfo> MergeHead
{
get
{
return this._MergeHead;
}
set
{
if (value != null)
{
for (int i = 0; i < value.Count; i++)
{
if (value[i].ColIndex > this.Columns.Count || (value[i].ColIndex + value[i].ColCount) > this.Columns.Count)
{
throw new Exception("超出索引");
}
}
this.MergeHead.Clear();
this._MergeHead.AddRange( value);
}
}
}
提示错误为"AddHeaderInfo[]”的对象无法转换为类型“AddHeaderInfo[]”。
//属性类
public class AddHeaderInfo
{
protected int _ColIndex=0;
public virtual int ColIndex
{
get { return this._ColIndex; }
set
{
if (value < 0)
{
throw new Exception("列索引不能为负~~");
}
else
{
this._ColIndex = value;
}
}
} protected int _ColCount=2;
public virtual int ColCount
{
get { return this._ColCount; }
set
{
if (value < 0)
{
throw new Exception("需要合并的列数量不能为负");
}
else
{
this._ColCount = value;
}
}
}
protected string _HeadText="1212";
public virtual string HeadText
{
get { return this._HeadText; }
set { this._HeadText = value; }
}
}下面是调用类 public partial class CustomControl2 : DataGridView,ISerializable
{
public CustomControl2()
{
InitializeComponent(); this._MergeHead=new List<AddHeaderInfo>();
this.CellPainting += new DataGridViewCellPaintingEventHandler(CustomControl2_CellPainting);
this.Scroll += new ScrollEventHandler(CustomControl2_Scroll); }
protected List<AddHeaderInfo> _MergeHead;
public virtual List<AddHeaderInfo> MergeHead
{
get
{
return this._MergeHead;
}
set
{
if (value != null)
{
for (int i = 0; i < value.Count; i++)
{
if (value[i].ColIndex > this.Columns.Count || (value[i].ColIndex + value[i].ColCount) > this.Columns.Count)
{
throw new Exception("超出索引");
}
}
this.MergeHead.Clear();
this._MergeHead.AddRange( value);
}
}
}
this._MergeHead.AddRange( value);
------------------------
字段和属性不能混着用
this.Scroll += new ScrollEventHandler(CustomControl2_Scroll);
这2个事件先去掉看看
{
if (value[i].ColIndex > this.Columns.Count || (value[i].ColIndex + value[i].ColCount) > this.Columns.Count)
{
throw new Exception("超出索引");
}
}先把value[i]转换为AddHeaderInfo 类AddHeaderInfo ahi = (AddHeaderInfo)value[i];
if (ahi.ColIndex > this.Columns.Count || (ahi.ColIndex + ahi.ColCount) > this.Columns.Count)
{
throw new Exception("超出索引");
}
不是这个问题 ,你得到的每一个每一个每一个每一个value都是一个AddHeaderInfo 类型,不需要转换
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.aspx