The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Adding entities to an esEntityCollection not updating bound control

Last post 03-19-2007, 5:48 AM by Scott.Schecter. 5 replies.
Sort Posts: Previous Next
  •  03-01-2007, 2:38 PM 694

    Adding entities to an esEntityCollection not updating bound control

    I've got a very simple test:

    Code:
    1    public LeftView()
    2    {
    3        InitializeComponent();
    4    
    5        products = new ProductCollection();
    6        productBindingSource.DataSource = products;
    7    }
    8    
    9    private void button1_Click(object sender, EventArgs e)
    10   {
    11       products.AddNew();
    12   }
    


    No matter how many times I click that button... I don't see any new entities in the various temp controls I have bound to productBindingSource.  I've tried calling EndEdit() after I call AddNew() but it makes no difference.  I've tried calling refresh on the controls... no difference.

    The binding *sorta* works, that is if I call LoadAll() on the collection, I will see the entities that are loaded from the DB, it's the new, in memory ones that aren't showing up. 

    I must be missing something very obvious.

    Any tips greatly appreciated,
    Steve
     

  •  03-01-2007, 8:33 PM 701 in reply to 694

    Re: Adding entities to an esEntityCollection not updating bound control

    Code:
    Bindingsource.EndEdit()
    control -> bindingsource -> in memory object
    
    BindingSource.ResetBindings(false)
    in memory object -> bindingsource -> control
    
    private void button1_Click(object sender, EventArgs e)
    {
        products.AddNew();
        productBindingSource.ResetBindings(false);
    }
    
    -OR-
    
    private void button1_Click(object sender, EventArgs e)
    {
        productBindingSource.AddNew();
    }
    

    David Neal Parsons
    www.entityspaces.net
  •  03-05-2007, 4:33 PM 774 in reply to 701

    Re: Adding entities to an esEntityCollection not updating bound control

    HI David,

    Thank you for the response.  Unfortunately this doesn't solve the problem for me.  When I initially load the EntityCollection and bind it to the BindingSource it displays the 2 items I have saved in the DB.  Once I add a new item to the collection and call BindingSource.ResetBindings(false) the ListBox control goes empty.  I set a breakpoint and checked the Count property of the BindingSource.DataSource and it shows the amount I would expect.

    Calling BindingSource.AddNew() works, but isn't a good option for me for a couple different reasons.

    Can you think of anything else that could cause the kind of behavior I'm seeing?  I' not saving the new entities to the DB, could that matter?  I mean, I will save them if the user clicks save, but as I'm creating them I'm not saving them.

  •  03-05-2007, 4:46 PM 775 in reply to 774

    Re: Adding entities to an esEntityCollection not updating bound control

    I've narrowed down the problem.  I get the expected bahvior if I create an EntityCollection and bind to a BindingSource.  If I call LoadAll() on the collection before binding, things down work the same.

     

    For example:

    Code:
    1    IFProtocolCollection _protocols = new IFProtocolCollection();
    2    
    3    public LeftView()
    4    {
    5        InitializeComponent();
    6        //_protocols.LoadAll();
    7        bindingSource1.DataSource = _protocols;
    8    }
    

     

    And in by button click handler:

    Code:
    1    private void button1_Click(object sender, EventArgs e)
    2    {
    3        IFProtocol protocol = _protocols.AddNew();
    4        protocol.Name = "ProtTest";
    5        bindingSource1.ResetBindings(false);
    6    }
    


    If I change the first part and remove the comments on the LoadAll() line, the DataBound ListBox doesn't display anything (even though there are entities in the collection) after I click the "Add Protocol" button.

    I hope this is a good clue for you.  I really need to get this stuff sorted out, I've been trying to get a simple ListBox bound to a collection working for almost a week  Tongue Tied


    Thanks for any suggestions or tips,
    Steve

  •  03-05-2007, 9:44 PM 781 in reply to 775

    Re: Adding entities to an esEntityCollection not updating bound control

    The clue is that it is a ListBox. A ListBox behaves more like a ComboBox than a DataGridView. First, I would not bother with a BindingSource. Second, if you want to change items in the ListBox, I would load it differently.  From MSDN:

    "There are two ways to fill the ComboBox and ListBox controls.

    For example, you can add objects to the ComboBox by using the Add method. You can also add objects to a ComboBox by using the DataSource, DisplayMember, and ValueMember properties to fill the ComboBox.

    When the DataSource property is set, a user cannot modify the items collection."

    Code:
    private ProductCollection products = new ProductCollection();
    
    private void FormCategoryTest_Load(object sender, EventArgs e)
    {
    	products.LoadAll();
    	foreach (Product prod in products)
    	{
    		listBox2.Items.Add(prod);
    	}
    	listBox2.DisplayMember =
    	  ProductMetadata.PropertyNames.Description;
    	listBox2.Sorted = true;
    }
    
    private void buttonAdd_Click(object sender, EventArgs e)
    {
    	Product prod = products.AddNew();
    	prod.Description = "My New Product";
    	prod.SubCategoryId = 1;
    	listBox2.Items.Add(prod);
    }

    David Neal Parsons
    www.entityspaces.net
  •  03-19-2007, 5:48 AM 1132 in reply to 781

    Re: Adding entities to an esEntityCollection not updating bound control

    David is spot on as per usual;) I deal with this in ASP.NET databinding all the time. For instance there is a combo box of widgets. The client will quite often want the top choice to be -Select One-, or All Widgets. As stated if you actually databind you cannot add these at runtime, it will throw an exception or simply be tossed out depending on the sequence of your events. The appropriate thing to do in this situation when you need to manipulate the items is to use the .Add or .Insert to populate the list based control manually. You can then manipulate the items as needed.

    Regards,

    Scott Schecter
    EntitySpaces | My Site
    Filed under:
View as RSS news feed in XML