Vivendi
Vivendi

Reputation: 21007

Use reference to sub object in GridView

I have a GridView control on my ASPX page. It works fine until i try to get a certain property from my object list.

I insert an object like this in a list:

var test = new List<DataTest>
{
    new DataTest
        {
            Name = "A name",
            Bla = new Bla()
        }
}

The class Bla looks like this:

public class Bla
{
    public string Blaat = "aaa";
}

But it seems i'm not able to get the property Blaat in a GridView, like this:

<asp:BoundField DataField="Bla.Blaat" HeaderText="Name" />

I can however output Name with no problem:

<asp:BoundField DataField="Name" HeaderText="Name" />

Does this mean i can only use properties that are directly defined in the DataTest object? So no reference to any sub object, like Bla.Blaat ?

Upvotes: 0

Views: 126

Answers (1)

Echilon
Echilon

Reputation: 10244

You'll need to use a TemplateField:

<asp:TemplateField HeaderText="Name" SortExpression="Name">
        <ItemTemplate>
            <asp:Label runat="server" ID="lblName" Text='<%# Server.HtmlEncode(Convert.ToString(Eval("Bla.Blaat"))) %>' />
        </ItemTemplate>
</asp:TemplateField>

You could also set the value in the code-behind.

Upvotes: 1

Related Questions