locked
EntityCollection Add() doesn't show record in DataGrid, AddNew() does RRS feed

  • Question

  • Hi,

    As the title states, I'm having the issue that when I use Add() to add an entity to an entity collection, the DataGrid doesn't show it. If I use AddNew() it does show. I have to use Add() because I want to add an existing entity to the collection.

    In my example I have an Invoice that contains multiple InvoiceLines. However, the InvoiceLines can be entered into the system before the Invoice get created. So at the point in time the user creates the Invoice, I want to add all InvoiceLines that currently don't belong to an Invoice.

    My screen shows the Invoice details and a DataGrid with InvoiceLines. I want to populate the DataGrid with the existing InvoiceLines, but the user can also add additional lines.

    In my code, in the Invoice_Loaded method, I iterate all InvoiceLines where Invoice == null. Then I add these InvoiceLines to the Invoice using this.Invoice.Add(invoiceLine).

    What am I missing here?

    Saturday, December 8, 2012 2:23 PM

Answers

  • You're welcome, & I'm always happy to be corrected if I'm wrong. Sometimes I answer too quickly.

    "I'm trying to associate an existing child with a parent"

    I understood what you were trying to do, but I allowed myself to be distracted by the use of Add/AddNew. The behavior that I was talking about, where the parent isn't set, is when you just new up an entity object.

    Actually, all you should need to do is to set the Invoice property on your InvoiceLine to the current Invoice.

    Just as I showed with creating a new record, you can do the same with the collection of unallocated invoiceLine as well:

    VB:

    For Each il in Me.InvoiceLines
        If (il.Invoice Is Nothing) Then
            il.Invoice = Me
        Endif
    Next

    C#:

    foreach (var il in this.InvoiceLines)
    {
        if (il.Invoice == null)
        {
            il.Invoice = this;
        }
    }

    Now as far as what shows up in the grid when, even after a grid collection Refresh, there's some weird behavior in the DataGrid, that I've called a bug since I first noticed it. But the team have stated that it's intended behavior.

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    • Proposed as answer by Angie Xu Thursday, December 20, 2012 1:00 AM
    • Marked as answer by Angie Xu Friday, December 21, 2012 7:00 AM
    Thursday, December 13, 2012 2:25 AM
    Moderator

All replies

  • Are you setting the parent Invoice property on the InvoiceLine? AddNew does this for you, Add doesn't.

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Sunday, December 9, 2012 11:31 PM
    Moderator
  • I'm starting to believe I'm hitting a bug. I created a sample that demonstrates my issue. You can download the sample at the link below, called EntityCollectionAddSample.zip.

    Replicate my issue by following these steps:

    1. Launch the application and choose Tasks -> Create Invoice Lines
    2. Enter a description, e.g. 'This invoice line was added from the data workspace' and hit save
    3. Go to the Create Invoice screen and hit refresh
    4. Two records are shown in the data grid. The Invoice Line Count shows '3'

    What this sample does is, at the Created event of the screen:

    • Add a new invoice line through the AddNew() method
    • Add a new invoice line by instantiating the line and than adding it to the collection using the Add() method
    • Add the invoice line from the InvoiceLines of the ApplicationData data source using the Add() method. This line does not show in the data grid.

    So, my research tells me that unless an entity is created on the same screen, it won't show up in the data grid after adding it to the EntityCollection of a screen property, although it physically is added to the collection.

    Is this a bug or by design? How do I overcome this issue. The requirement is that the invoice lines are created at some other time, so I can't create them on the fly when creating the invoice. I'd also rather not copy the invoice line data to a new invoice line, because that would give me duplicate invoice lines in my database.

    https://skydrive.live.com/redir?resid=6A04382879C18DC5!1422&authkey=!ACXvFchgkanxK40


    • Edited by S. Schutten Monday, December 10, 2012 8:34 PM Fixed a few typos
    Monday, December 10, 2012 8:31 PM
  • I gave you the answer. AddNew creeates the relationship for you, while Add does not (you have to manually set the parent yourself). It's not a bug, it's the way it works.

    If you need to use Add, simply set the parent in your code with one line:

    var newEntity = new Entity();
    
    newEntity.Parent = this;
    //set the rest of the entity's properties however you want

    You can also use the other syntax if you need to initialise properties after adding it.

    this.EntityCollection.AddNew();
    
    var newEntity = this.EntityCollection.SelectedItem();
    
    newEntity.SomeProperty = someValue;
    //continue to initialise oter properties

    Either way will work.

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Tuesday, December 11, 2012 2:33 AM
    Moderator
  • Hi Yann,

    Thanks for trying to help here. I really appreciate your effort. I do have to correct you on this one, though :-)

    Maybe it's changed with Lightswitch v2, but if you use Add(), the relationship IS created and the parent is set. I confirmed this by setting a breakpoint and checking the Invoice property of the InvoiceLine before and after the Add().

    Notice that in the sample I posted I attempt to three methods to add InvoiceLines to the Invoice, two of which use Add(). The difference between these two is that the first adds an InvoiceLine that I create in code, just before the call to the Add() method and the latter uses an existing InvoiceLine from the data source. The first one shows up in the data grid, the latter doesn't. And they both are added using the Add() method without explicitly setting the parent Invoice!

    To be clear, I'm not trying to create a new record and add it to the collection, I'm trying to associate an existing child with a parent. Think of it like you have people in your database and then you want to have a screen where you could say which people are kids of what parent.

    Check out my sample, you'll see.

    Wednesday, December 12, 2012 7:56 PM
  • You're welcome, & I'm always happy to be corrected if I'm wrong. Sometimes I answer too quickly.

    "I'm trying to associate an existing child with a parent"

    I understood what you were trying to do, but I allowed myself to be distracted by the use of Add/AddNew. The behavior that I was talking about, where the parent isn't set, is when you just new up an entity object.

    Actually, all you should need to do is to set the Invoice property on your InvoiceLine to the current Invoice.

    Just as I showed with creating a new record, you can do the same with the collection of unallocated invoiceLine as well:

    VB:

    For Each il in Me.InvoiceLines
        If (il.Invoice Is Nothing) Then
            il.Invoice = Me
        Endif
    Next

    C#:

    foreach (var il in this.InvoiceLines)
    {
        if (il.Invoice == null)
        {
            il.Invoice = this;
        }
    }

    Now as far as what shows up in the grid when, even after a grid collection Refresh, there's some weird behavior in the DataGrid, that I've called a bug since I first noticed it. But the team have stated that it's intended behavior.

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    • Proposed as answer by Angie Xu Thursday, December 20, 2012 1:00 AM
    • Marked as answer by Angie Xu Friday, December 21, 2012 7:00 AM
    Thursday, December 13, 2012 2:25 AM
    Moderator