views:

602

answers:

1

For example, in my WCF, if I have a Customer table and an Order table that have an association to each other on Customer ID.

I want to get the Count of the number of Orders for each Customer.

In regular VB code I can just do this:

Dim CustID As Integer = 1234
Dim cust As New MyService.Customer()
cust = cust.GetCustomer(CustID)
Response.Write(cust.Orders.Count)

The above code works fine but if I want to access the same from within a GridView, it doesn't seem to react the same way (albiet, from a different method but I'm applying the same pricipals to each method though)

<asp:ObjectDataSource ID="odsCustomers" runat="server" SelectMethod="GetCustomers" TypeName="MyService.Customer" />

<asp:GridView ...>
...
<Columns>
    <asp:BoundField DataField="Orders.Count" HeaderText="Order Count" />
</Columns>
...
</asp:GridView>

So how could I do something like this?

+3  A: 

You could try adding a partial method to the Customer class called OrderCount and reference that in your BoundField. Right click you dbml file and go to View Code then add.

VB

Partial Public Class Customer

    Public ReadOnly Property OrderCount As Integer
        Get
            Return Me.Orders.Count
        End Get
    End Property

End Class

C#

public partial class Customer
{
    
    public int OrderCount {
        get { return this.Orders.Count; }
    }
}
Christopher Edwards
Any other solutions?I mean, is there any other way to set the value itself like in a repeater by server tags Text="<%# Eval( "Orders").Count %>"
Shimmy
Yeah<asp:templatefield HeaderText="Order Count"><itemtemplate><asp:Label ID="OrdersCountLabel" runat="server"Text='<%# Eval("Orders.Count") %>' /></itemtemplate></asp:templatefield>Should work.
Christopher Edwards