How to display properties of related objects, without creating a dedicated TypedList or Typed view????

Posts   
 
    
trevorg
User
Posts: 104
Joined: 15-Nov-2007
# Posted on: 29-Nov-2007 22:09:20   

This is not necessarily a LLBLGen problem, but it is something that I often run up against and thought someone may have found a nice solution to it.

When displaying a collection in a grid, is there an easy way to display the value of a property of a related object property of one of the elements in the collection? ie: OrdersCollection in grid but want to display order.Customer.CompanyName in the grid, rather than CustomerID.

Example:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ordersEntityCollection" 
        DataKeyNames="OrderId" AllowPaging="True" PageSize="5">
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:BoundField DataField="ShipAddress" HeaderText="ShipAddress" SortExpression="ShipAddress" />
            <asp:BoundField DataField="ShipName" HeaderText="ShipName" SortExpression="ShipName" />
            <asp:BoundField DataField="ShipCountry" HeaderText="ShipCountry" SortExpression="ShipCountry" />
            <asp:BoundField DataField="CustomerId" HeaderText="CustomerId" SortExpression="CustomerId" />
            <asp:BoundField DataField="ShipRegion" HeaderText="ShipRegion" SortExpression="ShipRegion" />
            <asp:BoundField DataField="ShipCity" HeaderText="ShipCity" SortExpression="ShipCity" />
            <asp:BoundField DataField="OrderId" HeaderText="OrderId" SortExpression="OrderId" />
            <asp:BoundField DataField="ShipPostalCode" HeaderText="ShipPostalCode" SortExpression="ShipPostalCode" />
        </Columns>
    </asp:GridView>

In the above example, I would like to replace: <asp:BoundField DataField="CustomerId" HeaderText="CustomerId"/> With something like: <asp:BoundField DataField=".Customers.CompanyName" HeaderText="Customer Name"/>

So instead of the CustomerID being shown, we can see their name. True, a simpler (and more efficient) way of doing it would be to define a Typed List, however, in one-off situations I would rather not have to define a new list, not to mention that they are read only.

Any ideas?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 30-Nov-2007 07:27:20   

Hi trevorg, you have some options:

A. Explicity access the property at GUI:

<asp:Label ID="lblCustomer" runat="server" Text='<%# Eval("Customers.CompanyName") %>'/>

B. Create a custom property in a partial class or in the USER_CODE_REGIONS that return the required property, then access that property at GUI, as you suggest:

At the partial class:

public partial class OrderEntity
{
    public string CustomerCompany
    {
        get
        {
            string toReturn = string.Empty;

            if (Customers != null)              
            {
                toReturn = Customers.CompanyName;   
            }

            return toReturn;
        }
        
    }
}

and at your GRID:

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

C. At LLBLGenPro Designer, add a field mapped on relations (see docs for more information), at the GUI is the same as B.

Hope helpful wink

David Elizondo | LLBLGen Support Team
trevorg
User
Posts: 104
Joined: 15-Nov-2007
# Posted on: 30-Nov-2007 18:46:00   

Thanks Daelmo,

I checked out the "field mapped on relations" (in the GUI, the "Fields on Related fields" tab in entity properties screen....very nice.

Option A looks like a fairly decent way of doing this in an ASP page in an adhoc manner.

1) Is there a similar way of doing this on a windows form?

2) Is there a way to do this in a more generic fashion (ie: extend the ASP.Net Gridview) I was thinking that one could override an event such as RowDataBound and for each control in the datarow, examine the DataField proeprty looking for a ".", and if found, then populate the control with the value of the property specified, otherwise just databind normally.

I tried to figure this out but couldn't quite get there.

And if this more generic way was to work, is there something similar that could be done in a windows form grid?

Thanks for your help.

PS: Is it possible to get a short extension on my demo? I'm pretty sure I'm going to be buying a license, but it will take a bit of a delay to get my employer to pay for it rather than me.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 01-Dec-2007 01:18:36   

trevorg wrote:

1) Is there a similar way of doing this on a windows form?

In WinForms you can use B and C.

trevorg wrote:

2) Is there a way to do this in a more generic fashion (ie: extend the ASP.Net Gridview) I was thinking that one could override an event such as RowDataBound and for each control in the datarow, examine the DataField proeprty looking for a ".", and if found, then populate the control with the value of the property specified, otherwise just databind normally.

I think that's possible. LLBLGenPro Framework is very powerful and you can access so much information as PrimaryKeys, ForeignKeys, DataTypes, etc., so that's possible but you have to implement by yourself. Other thing you can do is write your own templates to write your GUI. LLBLGenPro dev team made "LLBLGenPro GUI Templates" that generates ASPX GUI templates. Is such templates you can define things like "when to put FK controls", "what is the descriptive field of a table", etc. wink

trevorg wrote:

PS: Is it possible to get a short extension on my demo? I'm pretty sure I'm going to be buying a license, but it will take a bit of a delay to get my employer to pay for it rather than me.

I'll redirect your request to LLBLGenPro Team.

David Elizondo | LLBLGen Support Team
Aglaia avatar
Aglaia
LLBLGen Pro Team
Posts: 535
Joined: 07-Sep-2003
# Posted on: 01-Dec-2007 12:22:51   

trevorg wrote:

PS: Is it possible to get a short extension on my demo? I'm pretty sure I'm going to be buying a license, but it will take a bit of a delay to get my employer to pay for it rather than me.

I just emailed the files necessary to extend the trial period to the address you used to register to the forums.

Posts: 254
Joined: 16-Nov-2006
# Posted on: 01-Dec-2007 23:34:46   

In relation to the original question in this thread I would simply define a TemplateField within the GridView as

<asp:TemplateField HeaderText="Customer Name"><ItemTemplate><%# Eval("Customers.CompanyName") %></ItemTemplate></asp:TemplateField>

This should achieve what you need to do.