Collections and DataBinding

Posts   
 
    
tedh
User
Posts: 34
Joined: 14-Dec-2006
# Posted on: 09-Jan-2007 00:39:39   

Environment: .Net 2.0 SelfServicing

I have the following:

EmployeeCollection employees = new EmployeeCollection();

Now employees is bound to a BindingSource and BindingSource is bound to a customized NavigationBar component.

I am using the BindingSource MoveFirst(), MovePrevious(), etc. commands to move through the BindingSource and display the appropriate records (one at a time) in sequence.

Now employees can be deleted from EmployeeCollection(). I do this using employees[index].Delete(). I have discovered that when I delete employees from the collection that the Count for the EmployeeCollection and also the BindingSource do not change.

When I move through the BindingSource I am always checking the current position against Count. When I hit the end I want to wrap back to the first record in the BindingSource.

My problem is that this logic does not easily work when the last records in the BindingSource are really deleted entities and they count as part of Count.

For example I have 5 employees in the employees collection. I delete employees 4 and 5 (the last two employees). Now I am at Position 2 (employee 3) in the BindingSource. Since I can't use Count to determine if I am at the end how do I easily determine that I have to wrap back to employee 1. I am interested to know the recommended practise for this scenario.

Thanks

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 09-Jan-2007 03:28:11   

Are you able to rebind after a delete? Then you would have the updated count.

tedh
User
Posts: 34
Joined: 14-Dec-2006
# Posted on: 09-Jan-2007 04:04:21   

My project is WinForms. Isn't DataBind() for ASP.Net? I thought that if a BindingSource is connected to an EntityCollection then changes in one were normally automatically communicated to the other?

Can you please tell me what are the rules regarding when to rebind and what is the command to be used and on which object? I can't find a DataBind() or ReBind() commands for the BindingSource or for the EntityCollection. I did find ResetBindings() but this doesn't resolve the problem.

Thanks

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 09-Jan-2007 09:07:12   

Now employees can be deleted from EmployeeCollection(). I do this using employees[index].Delete()

To remove an EntityFrom a Collection use any of the Remove methods of the Collection.

tedh
User
Posts: 34
Joined: 14-Dec-2006
# Posted on: 09-Jan-2007 16:06:00   

Thank you. That was exactly what I needed.