Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem with binding to List(Of ISomething).
I currently have a SalesOrder Class that has a
Property OrderDetails As List(Of IOrderDetail) Implements IOrder.OrderDetails.
The problem is that when I bind this list to a DataGridView I only get to see the columns of Properties defined in the Interface. That's expected behaviour, but I would like to see the properties of my SalesOrderDetail, not the IOrderDetail.
I have tried CTyping the List(Of IOrderDetail) to List(Of SalesOrderDetail), but it seems that CTyping List(Of T) is not allowed. List(Of IOrderDetail) cannot be converted to List(Of SalesOrderDetail), though both are IOrderDetail and the List(Of IOrderDetail) actually contains SalesOrderDetails...

Another problem I have is that my OrderDetail has a Property Product As IProduct Implements IOrderDetail.Product. I have bound my SalesOrder to a DataGridView, but I cannot bind a Class to a DataGridViewColumn (the Product Property), so I want the DataGridViewColumn to show the Name Property of the Product instead... (So I would actually be binding a Property of a Property).

Any help is appreciated :)
Posted
Updated 25-Mar-11 9:52am
v4
Comments
willempipi 25-Mar-11 15:19pm    
We need more information, what is "SalesOrderDetail"??

Can you give some more information about the class designs?
Sander Rossel 25-Mar-11 15:57pm    
A SalesOrderDetail is exactly what it says. A SalesOrderDetail implementing IOrderDetail. The SalesOrder (implementing IOrder) has a Property which is a List(Of IOrderDetail).
Furthermore my SalesOrderDetail has a Property Product As IProduct (which is given a derived Class of IProduct).
That's all there is to it :)

1 solution

With respect to your first question: you shouldn't try to do this. If you pass in a concrete class that doesn't have the required properties you'll get an exception. I suggest you create your own control for the Interface, then specialize it for the concrete class you want to use.

With respect to your second question, you can set up an object data source and bind to that or you can take a look at http://msdn.microsoft.com/en-us/library/y0wfd4yz.aspx[^]. Binding an individual object to a list control doesn't make much sense however.


Hope this helps!
 
Share this answer
 
Comments
Sander Rossel 25-Mar-11 15:53pm    
Thanks for your answer. I am not quite sure if I understand what you mean. I should be able to CType an Interface or BaseClass to a derived Class if I first put in a derived Class.
So the following would be completely ok.

Dim order As IOrder = New SalesOrder
CType(order, SalesOrder).SomethingSalesOrderSpecific()

That would only go wrong when I would say something like Dim order As IOrder = New PurchaseOrder and then CType to SalesOrder.
So that is why I was quite surprised to see that I could not CType my List(Of IOrder) to a List(Of DerivedOrder).

As for the second question I now see that it was a bit off. I have updated it. Sorry for the inconvenience.
Keith Barrow 25-Mar-11 19:27pm    
So that is why I was quite surprised to see that I could not CType my List(Of IOrder) to a List(Of DerivedOrder).
- That is because you cannot guarantee at compile time that all elements in the List(Of IOrder) are really of type DerivedOrder. (Even if they actually are for all instances you have)
Sander Rossel 20-Apr-11 10:56am    
Sorry I took so long to answer... Kind of forgot about this question. I understand the problem now though. Thanks for your good answer!
Keith Barrow 21-Apr-11 7:05am    
Thanks for replying anway, glad you got it sorted!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900