"gridview Object does not match target type" error during binding collection of different type objec

G

gui.besse

It seems that we can't bind a collection of instance of different type.

Let's have an example:

// My POCO
public interface ITest
{ string Name { get;set;} }

public class A : ITest
{
private string _name;
public string Name
{
get{ return _name;}
set{ _name = value;}
}

public class B : ITest
{
private string _name;
public string Name
{
get{ return _name;}
set{ _name = value;}
}

// My asp.net page
// 1 - populate my list
List<ITest> myList = new List<ITest>();
A a = new A();
a.Name = "my name is A";
myList.Add(a)

B b = new B();
b.Name = "my name is B";
myList.Add(b);

// the binding with gridview
myGridView.DataSource = myList:
myGridView.DataBind();

generate an exception on the last line:

Object does not match target type.
Exception Details: System.Reflection.TargetException: Object does not
match target type.
[TargetInvocationException: Property accessor 'Name' on object 'B'
threw the following exception:'Object does not match target type.']

Any idea?
 
Joined
Jul 7, 2006
Messages
1
Reaction score
0
Same problem with DataGrid

Hi,

I have a similary problem with a DataGrid bounded to an ArrayList containing several object classes. All these classes are sub-classes of a parent class.

e.g.:

public class A
{
public virtual string Name
{
get {return string.Empty;}
}
}

public class AA : A
{
private string name;
public override string Name
{
get {return name;}
}
}

public class AB : A
{
private string fullName;
public override string Name
{
get {return fullName;}
}
}


If my ArrayList contains a class AA object and a class AB object, when the AA object if bounded, the Name property is used to bind a column of the DataGrid : everything works fine. When the AB object is then bounded, I catch a TargetInvocationException with the message:
Property accessor 'Name' on object 'AB' threw the following exception:'Object does not match target type.

Here is the full stack trace:

[TargetException: Object does not match target type.]
System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess) +0
System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean verifyAccess) +422
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +23
System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +14
System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component)

[TargetInvocationException: Property accessor 'Name' on object 'AB' threw the following exception:'Object does not match target type.']
System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component)
System.Web.UI.WebControls.ButtonColumn.OnDataBindColumn(Object sender, EventArgs e)
System.Web.UI.Control.OnDataBinding(EventArgs e)
System.Web.UI.Control.DataBind()
System.Web.UI.Control.DataBind()
System.Web.UI.Control.DataBind()
System.Web.UI.WebControls.DataGrid.CreateItem(Int32 itemIndex, Int32 dataSourceIndex, ListItemType itemType, Boolean dataBind, Object dataItem, DataGridColumn[] columns, TableRowCollection rows, PagedDataSource pagedDataSource)
System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource)
System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e)
System.Web.UI.WebControls.BaseDataList.DataBind()
System.Web.UI.Control.DataBind()
TNS.Framework.Production.Web.Controls.SupportFinderControl.DataBind() in ...\myPage.cs:107
TNS.Framework.Production.Web.Controls.SupportFinderControl.btnSearch_Click(Object sender, EventArgs e) in ...\myPage.cs:258
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()


I Think that the DataGrid instanciate a ReflectPropertyDescriptor of the first object, with its concrete class (AA), and uses this instance while binding the second object, so the ReflectPropertyDescriptor does not match the correct class of the second object (AB).

Did you manage to resolve your problem ? If yes, how ?

Thanks for reply.
 
Joined
Aug 6, 2006
Messages
1
Reaction score
0
Databinding against heterogeneous collections

I've had this problem, and found two solutions: either implement ITypedList, or make your collection homogeneous. The second option is not so distasteful as it may sound, because you can automate the process of generating a suitable type to represent commonalities among the objects you are databinding.

You can find more information including an implementation of this second idea on my DefensiveDatasource page.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top