Bind GridView to generic custom object List collection

S

Steven Baggs

Hi!,

I have defined a List<T> like this List<Container>
Container class holds my Item object and it's state as a Enum.

Container.Item
Container.State

I would like to bind it to GridView.

gridview1.DataSourde = list;
gridview1.DataBind();

Problem is that I can't get it to show the data. I try:
<asp:BoundField DataField="Item.Name" HeaderText="Name" />

but I'm getting an error saying that Item doesn't exist.

Right now I'm using DataSet but I would like to use my business object's
collection.

Best regards,

Steven
 
T

Tod Birdsall, MCSD for .NET

Hi Steven,

When I bind custom collections to GridViews, I use the following:
<asp:BoundField DataField="State" HeaderText="State"></asp:BoundField>

or:

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

The above assumes that your custom collection contains an has a
"State" property that returns a string.

Hope this helps.

Tod Birdsall, MCSD for .NET
blog: http://tod1d.blogspot.com
 
J

Joe Dotnet

Thanks for the reply. I have to check is, in my case, Item an property. It
might be a public variable.
 
S

Steven Baggs

well it wasn't a property. Now I can access the 'Item' object.
How can I access further - the ExpirationDate property of the Item object?

is something like that valid?
<asp:BoundField DataField="Item.ExpirationDate" HeaderText="Expiration Date"
/>

Right now I'm getting:

A field or property with the name 'Item.ExpirationDate' was not found on the
selected data source.
 
X

xhead

It sounds like this should work, no?

<asp:BoundField DataField="ExpirationDate" HeaderText="Expiration
Date"/>

or maybe this:

<asp:TemplateField HeaderText="Expiration Date">
<ItemTemplate>
<%# Eval("ExpirationDate") %>
</ItemTemplate>
</asp:TemplateField>

Mike
 
S

Steven Baggs

ExpirationDate is a property of the Item object which is a property of a
ShoppingMetaElement object :)

I found this working Text= '<%# Eval("Item.ExpirationDate") %>' in the
TemplateField.

Seems impossible in DataBound column.

BTW: MSDN beta documentation of ASP.NET has a lot of bugs.
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top