Reputation: 1448
Hey,
I would like to show the title and the price of a movie in my Gridview. The title and price are properties from the class Movie and Movie is a property of the class CartItem.
Here is the Code of my gridview
<asp:GridView ID="gvShoppingCart" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataField="Price" HeaderText="Price" />
</Columns>
</asp:GridView>
The DataSource of my gridview is List<CartItem>
.
This are the classes
public class CartItem
{
private Movie _movieInCart;
private int _Count;
public CartItem()
{ }
public CartItem(Movie movie, int count)
{
_movieInCart = movie;
_count= count;
}
public Film MovieInCart
{
get { return _movieInCart; }
set { _movieInCart = value; }
}
public int Count
{
get { return _count; }
set { _count = value; }
}
public double getSubTotal()
{
return _movieInCart.Price * _count;
}
}
public class Movie
{
private string _title;
private double _price;
public string Title
{
get { return _title; }
set { _title= value; }
}
public double Price
{
get { return _price; }
set { _price= value; }
}
//More properties here
}
Apparently the GridView shows only first level properties, but how do I show these second level properties.
Many thanks, Vincent
Upvotes: 1
Views: 4361
Reputation: 1806
I think you have two options:
TemplateField
instead of BoundField
.Option 1:
public string Title
{
return _movieInCart != null ? _movieInCart.Title : null;
}
Option 2: (see reference)
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<asp:Label
runat="server"
Text='<%# Bind("MovieInCart.Title") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
Upvotes: 2