Binding GridView to ObjectDataSource with Complex Objects

NOVBusApps
NOVBusApps used Ask the Experts™
on
I'm working on a web application for managing purchase orders and I'm trying to bind some data to a gridview.  My main object is a PO object with basic PO header properties and a POLine object with basic PO line properties.  Each PO can have multiple lines so my PO object contains a List of POLine objects.

The trouble comes when trying to bind to a gridview.  I'm using an objectdatasource to bind to the gridview.  For this gridview I want to show 2 properties... PO Number (a property of the PO object) and Delivery Date (a property of the PO Line object).  I can bind to any of the PO properties just fine but I can't seem to figure out how to bind to a property of the PO Line object.  I've tried DEL_LINE_DATE, and I tried PO_LINES[0].DEL_LINE_DATE but both of these came back with this error... "A field or property with the name 'DEL_LINE_DATE' was not found on the selected data source"


public class PurchaseOrder
    {
        public string Po_Num { get; set; }       
        public List<PurchaseOrderLine> PO_Lines { get; set; }
     }
 
public class PurchaseOrderLine
    {
        public DateTime Del_Line_Date { get; set; }
    }
 
 
<asp:BoundField DataField="DEL_LINE_DATE" DataFormatString="{0:MM/dd/yyyy}" HeaderText="Delivery Date"
                        HtmlEncode="False" SortExpression="DEL_LINE_DATE" />

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Lead Software Engineer
Commented:
The object data source is binding to your PO object and only knows about the properties of the PO object. It will not know anything about the individual PO Line item objects. You can get around this issue by either adding a property to the PO object that will contain the DEL_LINE_DATE value (for which you'd need to determine which of the PO Line Items this date should be) or use the OnRowDataBound event and cycle through all of the PO Line Item objects attached to the PO object that is bound to that data row (e.DataItem).
Grant SpiteriSenior consultant

Commented:
My suggestion would be to not use an object data source and to bind in the code behind instead that will also make your life alot easier

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial