This project is read-only.

GridView and relation field

Apr 2, 2010 at 4:51 AM
Edited Apr 2, 2010 at 4: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)


    public class TurmaDisciplina :ISelfContainingObject

//all others variables and properties here

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

Turma Turma {
get { return _objectHolder.InnerObject; }
{ _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>();
<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:

<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?



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

Apr 3, 2010 at 12:02 AM
Edited Apr 3, 2010 at 12:04 AM
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?
Apr 4, 2010 at 12:01 AM

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

Apr 5, 2010 at 2:57 AM

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