Gridview databinding with nested types

R

Richard Gannon

Hi all,

Is there an *easy* way to use declarative databinding when biniding an
ASP.NET 2.0 Gridview to an ObjectDataSource that consumes custom
entities that contain nested types??

For Example, suppose I have the following classes in my BLL (Business
Logic Layer):
(Note: this is made-up code, but should demonstrate the issue).


public class Customer
{
string _customerTitle = "";
string _customerForename = "";
string _customerSurname = "";
Address _customerAddress = null;

public string CustomerTitle
{
get {return _customertitle;}
set {_customerTitle = value;}
}

public string CustomerForename
{
get { return _customerForename; }
set { _customerForename = value; }
}

public string CustomerSurname
{
get { return _customerSurname; }
set { _customerSurname = value; }
}

public Address CustomerAddress
{
get
{
if (_customerAddress == null)
_customerAddress = [Code to retrieve customer's
address from DAL];
return _customerAddress;
}

}
}


public class Address
{
string _address1 = "";
string _address2 = "";
string _address3 = "";
string _postcode = "";

public string Address1
{
get { return _address1; }
set { _address1 = value; }
}

public string Address2
{
get { return _address2; }
set { _address1 = value; }
}

public string Address3
{
get { return _address3; }
set { _address1 = value; }
}

public string Postcode
{
get { return _postcode; }
set { _postcode = value; }
}
}



As you can see, the "Customer" type contains a number of simple types
for the customer's forename, surname etc. and also contains a property
which exposes a "nested type" of the Address type.

In code, I can happily do things like the following:

_customer.Forename = "John";
_customer.Surname = "Smith";
_customer.Address.Address1 = "123 High Street";
_customer.Address.Postcode = "ZZ1 99C";

However, I'm fairly certain that when binding a "Customer" object (via
an ObjectDataSource) to a Gridview, and using declarative databinding
techniques, you *cannot* specify something like the following:
(Again, made-up code, but should demonstrate the issue)

<asp:GridView ID="grvCustomers" runat="server"
DataSourceID="odsCustomers">
<Columns>
<asp:BoundField DataField="CustomerTitle"
HeaderText="Customer Title"/>
<asp:BoundField DataField="CustomerTitle"
HeaderText="Customer Forename"/>
<asp:BoundField DataField="CustomerTitle"
HeaderText="Customer Surname"/>
<asp:BoundField DataField="Address.Postcode"
HeaderText="Customer Postcode"/>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="odsCustomers" runat="server"
SelectMethod="GetCustomers" TypeName="Customer">
</asp:ObjectDataSource>

Note the line that tries to use "Address.Postcode" as the value for
the "DataField" attribute. This will fail.


Is there an easy way to achieve this using declarative techniques?
I'm aware that I can programmatically bind the various "Customer"
properties and the properties of the nested "Address" type to the
Gridview's columns, but I'm looking for a way to achieve this without
having to resort to programmatic binding.


Thanks in advance.



Regards,
Richard.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top