代码如下:不论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;
}
} }
}

解决方案 »

  1.   

    绑定的时候需要集合中的数据类型相同,虽说new A1("A")和new A2("B")可以隐式转换成A,但是对象的类型是不同的,因此在集合进行绑定的时候出错。
      

  2.   

    老大的接受有点牵强,不过给了我启发,仔细查看msdn后,知道了结果,谢谢。MSDN中关于DataGrid.DataSource绑定到ArrayList的解释。也可以将 DataGrid 绑定到 ArrayList。ArrayList 的一个功能是它可以包含多种类型的对象,但当列表中的所有项与第一项具有相同的类型时,DataGrid 只能绑定到这类列表。这意味着所有的对象必须是同一种类型,或者必须从与列表中第一项相同的类继承。例如,如果列表中的第一项为 Control,则第二项可能为 TextBox(它从 Control 继承)。另一方面,如果第一项为 TextBox,则第二个对象就不可能是 Control。此外,ArrayList 在绑定时必须包含项目。空 ArrayList 会导致空网格。