views:

49

answers:

1

I have the following classes/interfaces. I'm getting an ArgumentException saying "Complex DataBinding accepts as a data source either an IList or an IListSource". But, I am setting it to an IList. What am I doing wrong?

public interface IOriginList : IList<IOriginEntry>
{
   ...
}

public class OriginList : Interfaces.IOriginList
{
   ...
}

// Binding code
IList<IOriginEntry> originList = new OriginList();
cboOrigin.DataSource = originList;
+1  A: 

I don't believe you can bind to a generic IList<>, only a non-generic IList.

Try this:

cboOrigin.DataSource = originList.ToArray();


* Edit *

Actually, the problem might be that the IList is of an interface type? Nowhere is IOriginEntry set to a concrete object, and you can't bind the combo box item to an interface.

You could also try this:

public class OriginEntry {
    public string Name {get; set;}
}

public interface IOriginList : IList<OriginEntry> {
    ...
}

public class OriginList : Interfaces.IOriginList {
   ...
}

// Binding code
IList<IOriginEntry> originList = new OriginList();
cboOrigin.DataBindings.Add(new Binding("SelectedValue", originList, "Name"));
James B
ToArray() is a bit heavy, just cast to IList.
Hans Passant
ToArray is not a member of IList. If I try and cast to a non-generic IList, I get an InvalidCastException.
bsh152s
That may work but I have no control over the IOriginList/OriginList/IOriginEntry/OriginEntry interfaces/classes (they are from a 3rd party). I was hoping I wouldn't have to iterate through the list and add the items manually.
bsh152s
ToArray is an extension method. Be sure to add a using for System.Linq
SnOrfus
Does your 3rd party have any examples of using their classes for databinding? Or a support forum?
James B
Weird situation. I have the source code, but I'd have to jump through hoops to get those in charge to change it. Looking at their code, they're looping through their items and adding them to their controls. I would like to try and avoid that, but maybe there's no way around it.
bsh152s
Not all that weird of a situation... Hoops are pretty standard in the industry :D Besides, changing code like that could have all sorts of repurcussions. I still think that ToArray() might be your only way to go. The code I wrote for an `OriginEntry` might work with `IOriginEntry`... what properties do you have that you could bind to instead of "Name"?
James B
Ended up using converting to concrete List using ToList and binding to it.
bsh152s