Adding objects to a ListBox in a loop

Adding objects to a ListBox in a loop

Old forum URL: forums.lhotka.net/forums/t/996.aspx


Michael Hildner posted on Wednesday, August 23, 2006

This may not be CSLA specific - thinking it's a databinding thing, just thought I'd throw it out there.

I have a ListBox that is whose DataSource is a BindingSource, the BindingSource's DataSource is an Editable Root List.

If I add a single item to the ListBox, via the bo's .AddNew() method, set my properties, the new item shows up just fine.

If I attempt add items in a loop (foreach or for...), the very last item added does not show it's text. The item is there, just with empty text. I can verify this by iterating through the items and printing out the text value.

Why would the last item to be added not show it's text?

Thanks,

Mike

 

RockfordLhotka replied on Wednesday, August 23, 2006

Just speculating here, but perhaps you are adding the item to the collection and then setting the text? If you set the text via the private field, rather than setting it via the public property, then the PropertyChanged event wouldn't fire.

Data binding drives off events from the list. When you add an item to the list, a ListChanged(added) is raised. Then, if you change the object via its properties, a ListChanged(changed) should be raised. But if you change the object without calling PropertyHasChanged() then this event wouldn't be raised and the list would never be notified that your object has a new value.

Michael Hildner replied on Wednesday, August 23, 2006

Thanks for the reply.

Yes, I am adding the item to the collection and then setting the text. More specifically, I'm setting the public property of the new object. That property does call PropertyHasChanged - stepped through the code to be sure. This is the same property that the ListBox's DisplayMember is set to (RoleName in this example).

Looking a little more carefully at this, it's not related to the looping thing. If I add an item one by one, sometimes the text appears, and sometimes it doesn't. Strange.

Here's the code if it makes a difference. I'm starting to think I'm just doing something wrong.

// Get the selected item.

ListViewItem item = lvRoleList.SelectedItems[0];

// Add new role to the assigned list.

EmployeeEditableChild employee = (EmployeeEditableChild)employeeEditableRootListBindingSource.Current;

EmployeeRoleEditableChild employeeRole = employee.EmployeeRoles.AddNew();

employeeRole.RoleFK = new Guid(item.Name);

employeeRole.EmployeeFK = employee.PK;

employeeRole.RoleName = item.Text;

// Remove from all roles list.

item.Remove();

Regards,

Mike

Brian Criswell replied on Thursday, August 24, 2006

I have run into similar behaviour on this.  In our case we were able to reset the DataSource property.  Try seeing if you can force a binding refresh.

Michael Hildner replied on Thursday, August 24, 2006

Cool, thanks Brian, that did the trick.

I set the ListBox's .DataSource to null, then back to the BindingSource - also had to reset the .DisplayMember so my wanted property is displayed.

Thanks!

Mike

 

RockfordLhotka replied on Thursday, August 24, 2006

You can probably just tell the bindingsource to resetbindings - there's a method for that.
 
Rocky


From: Michael Hildner [mailto:cslanet@lhotka.net]
Sent: Thursday, August 24, 2006 3:12 PM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] Adding objects to a ListBox in a loop

Cool, thanks Brian, that did the trick.

I set the ListBox's .DataSource to null, then back to the BindingSource - also had to reset the .DisplayMember so my wanted property is displayed.

Thanks!

Mike

 




Michael Hildner replied on Thursday, August 24, 2006

Thanks, that works as well - a couple less lines of code.

Thanks,

Mike

Copyright (c) Marimer LLC