GridView and relation field

Apr 2, 2010 at 3:51 AM
Edited Apr 2, 2010 at 3:53 AM

Hi, All!

I have two entities (Turma ad TurmaDisciplina) and Im doing an relation between them. I want to put a objectset inside an gridview doing this.

 

            GridView1.DataSource  = context.GetObjectSet<TurmaDisciplina>();

 

This objectset<TurmaDisciplina> stores only keys.

In persistent object I have the relation between theses entities and one property  inside TurmaDisciplina thats returns and Object Turma (filled in relation)

 

    [Persistent("turmadisciplina")]
    public class TurmaDisciplina :ISelfContainingObject
    {

//all others variables and properties here


        [Relation("IdTurma = IdTurma")]
        private ObjectHolder<Turma> _objectHolder = new ObjectHolder<Turma>();

public
Turma Turma {
get { return _objectHolder.InnerObject; }
set
{ _objectHolder.InnerObject = value; }
}

 

 

The object returned (Turma), have some properties (NomeTurma, IdTurma, etc)

Ex.: Turma.NomeTurma; Turma.IdTurma etc

If I put AutoGenerateColumns= true its fills the gridview with all keys storend in TurmaDisciplina object  and in the Turma column its puts the namespace of Turma class.

If I set up gridview to AutoGenerateColumns= false I can acess all properties from TurmaDisciplina and put them inside and BoundField.

 

            GridView1.DataSource = context.GetObjectSet<TurmaDisciplina>();
                GridView1.DataBind();
<Columns> <asp:BoundField DataField="Vagas" HeaderText="Vagas" /> <asp:BoundField DataField="IdPeriodoLetivo" /> </Columns>

 

 

My question is: how do I put the properties of this "secondary" object  (Turma) inside an gridview. For example, i want to put the propety Name of Turma object inside this gridview. Im tried to do this:

                            <Columns>
<asp:BoundField DataField="Vagas" HeaderText="Vagas" />
                                <asp:BoundField DataField="IdPeriodoLetivo" />


<asp:BoundField DataField="Turma[NomeTurma]" /> <asp:BoundField DataField="Turma.NomeTurma" /> </Columns>

But it does not work. Its returns-me and exception saying:  A field or property with the name 'Turma[NomeTurma]' was not found on the selected data source. Or A field or property with the name 'Turma.NomeTurma' was not found on the selected data source.

 

 

How can I do this?

 

thanks!

<asp:BoundField DataField="Vagas" HeaderText="Vagas" />
<asp:BoundField DataField="IdPeriodoLetivo" />

Apr 2, 2010 at 11:02 PM
Edited Apr 2, 2010 at 11:04 PM
I found a way! First I created an TempleateField and inside it I placed an label and filled the Text property like this: <Columns> <asp:TemplateField HeaderText="Turma"> <ItemTemplate> <asp:Label ID="lblTurma" runat="server" Text='<%# Eval("Turma.NomeTurma") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> So, can someonte tell me if this is the best way to do this? Its impossible to bind this object to a BoundField?
Coordinator
Apr 3, 2010 at 11:01 PM

This seems to be basic object binding. Please check out the .NET documentation on that...

Apr 5, 2010 at 1:57 AM

Binding the GridView control to a complex (deep) business object

http://weblogs.asp.net/lancea/archive/2004/08/04/207770.aspx