代码如下:不论datagrid.datasource绑定到al_A,还是al_B都报未处理的“System.Reflection.TargetInvocationException”类型的异常出现在 system.windows.forms.dll 中。其他信息: 对象“Test2.B2”上的属性访问器“BString”发生以下异常: “对象与目标类型不匹配。”真无奈呀
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace Test2
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); ArrayList al_A = new ArrayList();
al_A.Add(new A1("A"));
al_A.Add(new A2("B"));
al_A.Add(new A1("C"));
al_A.Add(new A2("D"));
al_A.Add(new A1("E"));
al_A.Add(new A2("F")); ArrayList al_B = new ArrayList();
al_B.Add(new B1("A"));
al_B.Add(new B2("B"));
al_B.Add(new B1("C"));
al_B.Add(new B2("D"));
al_B.Add(new B1("E"));
al_B.Add(new B2("F")); //this.dataGrid1.DataSource = al_A; this.dataGrid1.DataSource = al_B;
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(24, 8);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(240, 240);
this.dataGrid1.TabIndex = 0;
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 265);
this.Controls.Add(this.dataGrid1);
this.Name = "Form2";
this.Text = "Form2";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false); }
#endregion
} public interface B
{
string BString{get;set;}
} public class B1:B
{
public B1(string b)
{
this.b = b;
}
private string b; public string BString
{
get
{
return b;
}
set
{
b = value;
}
}
} public class B2:B
{
private string b;
public B2(string b)
{
this.b = b;
} public string BString
{
get
{
return b;
}
set
{
b = value;
}
}
}
public abstract class A
{
protected string a;
public A(string a)
{
this.a = a;
}
public abstract string AString
{
get;set;
}
} public class A2:A
{
public A2(string a):base(a)
{
} public override string AString
{
get
{
return a;
}
set
{
this.a = value;
}
} } public class A1:A
{
public A1(string a):base(a)
{
} public override string AString
{
get
{
return a;
}
set
{
this.a = value;
}
} }
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace Test2
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); ArrayList al_A = new ArrayList();
al_A.Add(new A1("A"));
al_A.Add(new A2("B"));
al_A.Add(new A1("C"));
al_A.Add(new A2("D"));
al_A.Add(new A1("E"));
al_A.Add(new A2("F")); ArrayList al_B = new ArrayList();
al_B.Add(new B1("A"));
al_B.Add(new B2("B"));
al_B.Add(new B1("C"));
al_B.Add(new B2("D"));
al_B.Add(new B1("E"));
al_B.Add(new B2("F")); //this.dataGrid1.DataSource = al_A; this.dataGrid1.DataSource = al_B;
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(24, 8);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(240, 240);
this.dataGrid1.TabIndex = 0;
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 265);
this.Controls.Add(this.dataGrid1);
this.Name = "Form2";
this.Text = "Form2";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false); }
#endregion
} public interface B
{
string BString{get;set;}
} public class B1:B
{
public B1(string b)
{
this.b = b;
}
private string b; public string BString
{
get
{
return b;
}
set
{
b = value;
}
}
} public class B2:B
{
private string b;
public B2(string b)
{
this.b = b;
} public string BString
{
get
{
return b;
}
set
{
b = value;
}
}
}
public abstract class A
{
protected string a;
public A(string a)
{
this.a = a;
}
public abstract string AString
{
get;set;
}
} public class A2:A
{
public A2(string a):base(a)
{
} public override string AString
{
get
{
return a;
}
set
{
this.a = value;
}
} } public class A1:A
{
public A1(string a):base(a)
{
} public override string AString
{
get
{
return a;
}
set
{
this.a = value;
}
} }
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货