data binding to a colleciton

D

Derrick

I have a form with a list box control and a few text
boxes. The controls are bound to a collection. All that
works fine when I load the data from the data base. When
the form loads, I call the 'intiallizeBindings' sub:
<snippet>
txtFirstName.DataBindings.Add("text",
g_Members, "FirstName")
lstMembers.DisplayMember = "FullName"
lstMembers.ValueMember = "ID"
lstMembers.DataSource = g_Members
lstMembers.DataBindings.Add("SelectedValue",
g_Members, "ID")
bmb = Me.BindingContext(g_Members)
bmb.Position = bmb.Count
bmb.Position = 0
</snippet>
I can navigate through the collection using my navigate
buttons or the clicking on the list box.

But when I add an item to the collection (g_members), it
does not refresh the list box or allow navigation to the
new items. Do I have to code something for an event in
the collection? or the binding manager base (bmb)?

Thanks for the help

Derrick
 
D

Derrick Repep

Derrick,

Check out the BindingManagerBase.SuspendBinding( ) and
BindingManagerBase.ResumeBinding( ) methods. When you are binding to a data
source that does not support the IBindingList interface (and I believe
collections and arraylists do not), this is the route that is suggested by
Microsoft (and that I have used as well).

Here's a link for more info.

http://msdn.microsoft.com/library/d...ingcomboboxcheckedlistboxorlistboxcontrol.asp

Good luck!

~Derrick
 
D

Derrick Underwood

Thanks for the help - now i have a new problem

When I add an object to my collection, I call 'suspendBinding' and
'resumeBinding' When I check the bindingManagerBase.count property, it
equals 3 - the correct number. While it appears to be there, it will not
allow me to navigate to the last item.
<snippet>
bmb.SuspendBinding()
g_Members.add(tm)
bmb.ResumeBinding()
MessageBox.Show("count:" & bmb.Count) ' displays the new count - ie.
3
MessageBox.Show("position: " & bmb.Position) ' displays current
postion - ie. 0
bmb.Position = bmb.Count - 1 ' causes a 4 second delay, but no
exception thrown
MessageBox.Show("position: " & bmb.Position) ' displays 0 - should
be 2
</snippet>

Thanks again for the help

Derrick
 
D

Derrick

Thanks for the help - now i have a new problem - with some more information

<<When I add an object to my collection, I call 'suspendBinding' and
'resumeBinding' When I check the bindingManagerBase.count property, it
equals 3 - the correct number. While it appears to be there, it will not
allow me to navigate to the last item. >>

The collection data is accurate. I can loop through the collection and get
all the info about the objects contained.

<snippet>
bmb.SuspendBinding()
g_Members.add(tm)
bmb.ResumeBinding()
MessageBox.Show("count:" & bmb.Count) ' displays the new count - ie.
3
MessageBox.Show("position: " & bmb.Position) ' displays current
postion - ie. 0
bmb.Position = bmb.Count - 1 ' causes a 4 second delay, but no
exception thrown
MessageBox.Show("position: " & bmb.Position) ' displays 0 - should
be 2
</snippet>

Is data binding with collections more trouble than it is worth (vs manually
filling the list box and text boxes), or am I just feeling the pain of doing
it for the first time?

Thanks again for the help

Derrick
 
Y

Ying-Shen Yu[MSFT]

Hi Derrick,

Thanks for choosing MSDN Newsgroup!

My name is Ying-Shen Yu, and I'll assist you on this issue.

From my understanding now, you bound several controls to a collection
object, the problem is when you added a new object to the datasource
collection, you still couldn't access the newly added collection. If I
misunderstood your problem, please feel free to let me know.

Based on my experience, I suspect this problem probably lies in the
internal state of CurrencyManager of the collection does not get updated
after adding a new object. Unfortunately, calling
SuspendBinding/ResumeBinding will not update these states, it only take
effects in the case you only changed some value (not add/remove). To make
your collection object fully support data-binding (add/remove/update), you
need implement IBindingList interface on your collection class and
IEditableObject interface on your collection element class. Implementing
these interface might take some time. However, comparing with filling
listbox textbox manually, we can void writing these code for a certain
control, it will make the code organized better and more reusable.

I had posted a sample code in MSDN library in the pervious issue
You may refer code on that post to get some idea on how to implement these
two interface.

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=8qSwEjP0DHA
.1464%40cpmsftngxa07.phx.gbl

If you have anything unclear on this issue, please be free to reply this
thread.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
D

Derrick

That your for the response. I found some solutions to the problems I was
having late last night, but I will look into your solution to see if it is a
better approach...

FYI, here was my solution:
- Problem: - adding item to collection did not add item to the data bound
controls. Solution: use the 'suspendBinding' and 'resumeBinding' methods in
the bindingManagerBase(bmb) object. I set the values of the 'firstName' and
'lastName' properties to default values in the 'new' method of the object so
the new and changed object would be recognized in the bmb object with the
'suspendBinding' and 'resumeBinding' methods were called.
- Next problem: item would be added to the collection, but could not be
navigated to. The cause was that one of the properties values was set to
'nothing'. After I set it to an empty string in the 'new' method, I could
navigate to the new element.

Thanks again for the help

Derrick


"Ying-Shen Yu[MSFT]" said:
Hi Derrick,

Thanks for choosing MSDN Newsgroup!

My name is Ying-Shen Yu, and I'll assist you on this issue.

From my understanding now, you bound several controls to a collection
object, the problem is when you added a new object to the datasource
collection, you still couldn't access the newly added collection. If I
misunderstood your problem, please feel free to let me know.

Based on my experience, I suspect this problem probably lies in the
internal state of CurrencyManager of the collection does not get updated
after adding a new object. Unfortunately, calling
SuspendBinding/ResumeBinding will not update these states, it only take
effects in the case you only changed some value (not add/remove). To make
your collection object fully support data-binding (add/remove/update), you
need implement IBindingList interface on your collection class and
IEditableObject interface on your collection element class. Implementing
these interface might take some time. However, comparing with filling
listbox textbox manually, we can void writing these code for a certain
control, it will make the code organized better and more reusable.

I had posted a sample code in MSDN library in the pervious issue
You may refer code on that post to get some idea on how to implement these
two interface.

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=8qSwEjP0DHA
.1464%40cpmsftngxa07.phx.gbl

If you have anything unclear on this issue, please be free to reply this
thread.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
Y

Ying-Shen Yu[MSFT]

Hi Derrick,

I'm glad to hear you have found a resolution to this issue.
If you have any questions or concerns in implementing the IBingList
,IEditableObject interfaces, please feel free to post it in this
newsgroup. I'm standing by to be of assistance.


Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top