Binding Objects

J

jerome.avoustin

Hi All ! :)

I got some pbs with binding objects in ASP.Net 2.0
I've got such an object composition :

class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Car Car { get; set; }
}

class Car
{
public string Type { get; set; }
public string Color { get; set; }
}

I build a List<Person> and try to bind it to a GridView
Here's my GridView :

<asp:GridView ID="gdvTest" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="Name" DataField="Name"/>
<asp:BoundField HeaderText="Age" DataField="Age"/>
</Columns>
</asp:GridView>

Then I try to bind values from the car
I try either this... :

<asp:BoundField HeaderText="Car Type" DataField="Car.Type"/>

.... Or this :

<asp:TemplateField HeaderText="Car Type">
<ItemTemplate>
<%# DataBinder.Eval(Container, "Car.Type") %>
Or
<%# DataBinder.Eval(Container,
"Personne.Car.Type") %>
</ItemTemplate>
</asp:TemplateField>

I always get an exception :

Impossible to find a fiels or a propertie 'Car.Type' in the datasource
or
DataBinding : 'System.Web.UI.WebControls.GridViewRow' has no propertie
calld 'Car'. (traduction from french)

Has someone any idea ?

Thanx ! :)
 
J

jerome.avoustin

It's OK !!
I've found the error !

You must use :

<asp:TemplateField HeaderText="Car Type">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,
"Car.Type") %>
</ItemTemplate>
</asp:TemplateField>

I've forgotten .DataItem with Container !
But in the FRENCH documentation, it is said :

"For any of the list Web controls, such as DataGrid, DataList, or
Repeater, container should be Container.DataItem."

And nothing about GridView !
But in the english documentation, GridView is mentionned !

Allright... I'll always read english documentation next times ! ;)

Thanx
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top