How to change values in one column in Master Detail Datagridviews?

Posts   
 
    
Dready
User
Posts: 9
Joined: 25-Oct-2008
# Posted on: 27-May-2009 19:38:18   

Hi,

I've got a .net 2.0 windows application that show a master detail relation between two DataGridViews. The detail view, that shows orderDetail data, now only contains a ProductID, which is not human readable. I would like to show a product description instead, but this is of course not saved in the orderDetail table.

This is how i get the orders and orderItems, prefetched (I use

public static EntityCollection getOrders()
        {
            EntityCollection orders= new EntityCollection(new OrderEntityFactory());
            IPrefetchPath2 path = new PrefetchPath2((int)EntityType.OrderEntity);
            path.Add(OrderEntity.PrefetchPathOrderItem);

            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                adapter.FetchEntityCollection(Orders, null, 0, null, path);
            }
            
            return Orders;
        }

And this is how i bind the Master and Detail Datagridviews;

bindingSource1.DataSource = BLL.OrderManager.getOrders();
dgvOrders.DataSource = bindingSource1;

dgvOrderDetails.DataSource = bindingSource1;
dgvOrderDetails.DataMember = "OrderItem";

This works fine, and the detail view shows the productID. But i'd like to replace the ProductID by a ProductDesription (which is a composition of a few columns in the product table. Or to add a new column runtime and set its value to the product description. But i cannot figure out how to do this, as the whole detail grid is generated by .net framework from the datasource. I did find that i can also prefetch the product info by adding this to getOrders, but i can't think of a way to add it to the detail view.

path.Add(OrderEntity.PrefetchPathProductCollectionViaOrderItem);

Any ideas on how to do this?

Thanks,

Dready

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 27-May-2009 20:44:12   

Hi Dready,

Glad you found the PrefetchPath to Product - that is good, as it will save you hits to the database.

What you're looking for is found in the LLBLGen Pro Designer. In the Designer, edit the OrderDetail entity & look for the tab: Fields on Related Fields. There, You are able to pull in fields from Related Parent Entities (ie, show Product Fields on your OrderDetail Entity).

Hope this helps!

Ryan

Dready
User
Posts: 9
Joined: 25-Oct-2008
# Posted on: 27-May-2009 23:51:48   

Hi Ryan,

Thanks for your reply! I added the fields I need from the product table to the 'Fields on Related Fields' and it got me a bit closer, as the fields do now show up as columns in the detail DataGridView. But strangely enough, they are always empty; And i checked that the corresponding entry in the Product table does exist.

Any idea why this would be?

Another question: It would already be great if i get this to work. But the product name is composite of a few fields in the product table. And i would prefer to add one column that concatenates these fields instead of the different columns seperately. How would this be possible?

Best,

Dready

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 28-May-2009 00:17:10   

Hi Dready,

1.) Empty Column: Verify your Product Entity is actually being fetched from the database. What might be happening is .Product may be returning a new Product entity instead of the fetched Product entity. This would indicate a problem with your PrefetchPath. In code, do:

Dim myOrder as OrderEntity = myOrders(0)
Dim myOrderDetail as OrderDetailEntity = myOrder.OrderDetail(0)

Dim myProduct as ProductEntity = myOrderDetail.Product

Verify that myProduct.isNew = False & that your myProduct.Description comes through. My hunch says .isNew = True; your Prefetch needs work.

I always turn off LazyLoadingWithoutResultReturnsNew in your LLBLGen's project Preferences - so that OrderDetail.Product NEVER returns a new Entity, it will return Nothing. Believe me - it will save you huge headaches.

2.) Calculated Fields: Add these to a partial class of your Entity. I do this all the time.

Namespace YourDataObjects.EntityClasses

    Partial Public Class OrderDetailEntity

        Public ReadOnly Property ProductNameAndDescription() As String
            Get
                Return Me.Product.Name & ", " & Me.Product.Description
            End Get
        End Property

Hope this helps!

Ryan

Dready
User
Posts: 9
Joined: 25-Oct-2008
# Posted on: 28-May-2009 00:26:56   

Hi Ryan,

You are right, myProduct is null. (I did switch LazyLoadingWithoutResultReturnsNew off). Mmm. not sure yet what to look at next.

The composite field idea will help a lot! Do you have to put it back every time you rerun the generator?

Thanks again!

Dready

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 28-May-2009 00:43:50   

Regenerating your project will work great. No need to worry. Assuming you simply put your Partial class in its own file - it will not be touched by the LLBLGen Pro Designer.

I simply put all my Partial classes in a Partial Classes folder within my projects.

Glad to help!

Ryan

Dready
User
Posts: 9
Joined: 25-Oct-2008
# Posted on: 28-May-2009 00:53:04   

Hi Ryan,

Could you give me a hint on how to try to fix my prefetch path? Do you think it's a problem in the Database (relations look OK to me), in the LLBLGen project, or in my getOrders() code?

The code looks like this. I also prefetch comments to the order for a second detail DataGridView. But this doesn't influence it, cause when i commented out that line, myProduct is still null....


EntityCollection myOrders= new EntityCollection(new OrderEntityFactory());
            IPrefetchPath2 path = new PrefetchPath2((int)EntityType.OrderEntity);           
            path.Add(OrderEntity.PrefetchPathOrderItem);            
            path.Add(OrderEntity.PrefetchPathProductCollectionViaOrderItem);
            path.Add(OrderEntity.PrefetchPathComments); 

            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                adapter.FetchEntityCollection(myOrders, null, 0, null, path);
            }

            OrderEntity myOrder = (OrderEntity)myOrders[0];
            OrderItemEntity myitem = myOrder.OrderItem[0];
            ProdcttEntity myProduct = myitem.Product;

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 28-May-2009 05:54:40   

Hi Dready,

I didn't read all the thread, but looking at your last post, this is the problem I see:

 path.Add(OrderEntity.PrefetchPathProductCollectionViaOrderItem);

Here you are prefetching the order->product m:n collection. And here:

OrderEntity myOrder = (OrderEntity)myOrders[0];
OrderItemEntity myitem = myOrder.OrderItem[0];
ProdcttEntity myProduct = myitem.Product;

you are accessing the product through order<-orderItem->product.

Said that, you have two options:

A. Prefetch as you are doing now, and access this way:


path.Add(OrderEntity.PrefetchPathProductCollectionViaOrderItem);
...
OrderEntity myOrder = (OrderEntity)myOrders[0];
ProdcttEntity myProduct = myOrder.ProductCollectionViaOrderItem[0];

B. Prefetch order<-orderItem->product graph, and access the objects the way you did at your last code snippet:

IPrefetchPath2 path = new PrefetchPath2((int)EntityType.OrderEntity);           
path.Add(OrderEntity.PrefetchPathOrderItem)
     .SubPath.Add(OrderItemEntity.PrefetchPathProduct);         
path.Add(OrderEntity.PrefetchPathComments); 
...
OrderEntity myOrder = (OrderEntity)myOrders[0];
OrderItemEntity myitem = myOrder.OrderItem[0];
ProdcttEntity myProduct = myitem.Product;

Hope helpful wink

David Elizondo | LLBLGen Support Team
Dready
User
Posts: 9
Joined: 25-Oct-2008
# Posted on: 28-May-2009 11:15:36   

Hi David,

Thanks for the suggestion. I cannot use Option A unfortunately, as i need the data for a master-detail grid, so it's not me deciding how to access the field. The way i access the field in the code snippet is only to test if any value is there.

I tried Option B, but myProduct is still null... Gonna keep on trying, but i really don't understand why this is happening.

Any ideas?

Dready.

Dready
User
Posts: 9
Joined: 25-Oct-2008
# Posted on: 28-May-2009 11:42:47   

Hi David and Ryan,

In the last post i just didn't pay attention, and it's now working. It's was too early in the morning and i didn't have my second coffee yet, so i wasn't looking straight (-:

Thanks so much to both of you,

Dready

Dready
User
Posts: 9
Joined: 25-Oct-2008
# Posted on: 28-May-2009 11:46:44   

David,

Not sure if i'm supposed to mark the thread done myself, but cannot find how. Maybe i need more coffee.

Dready

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 28-May-2009 17:24:19   

No, that's our job, but thanks anyway. I'll do it for you...simple_smile

Matt