Databound Listbox Does Not Display Changes to Datasource

G

Guest

A listbox, named 'lbxRooms', does not display changes when the datasource
bound to it changes. The revelant code:

Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnEdit.Click
DirectCast(lbxRooms.SelectedItem, DataRowView).Item("Name") =
Me.tbxName.Text
DirectCast(lbxRooms.SelectedItem, DataRowView).Item("Size") =
Me.numSize.Value
Dim bm As BindingContext = New BindingContext
Dim cm As CurrencyManager
cm = DirectCast(bm.Item(lbxRooms.DataSource), CurrencyManager)
cm.Refresh()
End Sub

The dataset does accept the changes, which I can confirm using another set
of controls that are grabbing data from the listbox's datasource:

Private Sub lbxRooms_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
lbxRooms.SelectedIndexChanged
tbxName.Text = DirectCast(lbxRooms.SelectedItem,
DataRowView).Item("Name").ToString
Me.numSize.Value = CInt(DirectCast(lbxRooms.SelectedItem,
DataRowView).Item("Size"))
End Sub

Please advise on how to get the listbox itself to display changes to its
datasource. Thank you.

--Noel
 
B

Bart Mermuys

Hi,
[Inline]

Noel Weichbrodt said:
A listbox, named 'lbxRooms', does not display changes when the datasource
bound to it changes. The revelant code:

Try:

Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnEdit.Click

Dim drv As DataRowView
drv = DirectCast(lbxRooms.SelectedItem, DataRowView)
drv.Item("Name") = Me.tbxName.Text
drv.Item("Size") = Me.numSize.Value
drv.EndEdit()

End Sub

HTH,
Greetings
 
N

Noel Weichbrodt

Hi Bart,
Thanks for the idea, but that solves the non-existent problem of the
textbox/numberbox not updating from the listbox's data. They are
updating fine. It's the listbox (in the first code sample) that is
having problems with not updating.

Unless you think that the reason the listbox is not updating is due
to the way I'm updating these unrelated controls, which, given the
finickiness of .net's databinding model, I would be fully prepared to
accept.

--Noel
 
B

Bart Mermuys

Hi,

Noel Weichbrodt said:
Hi Bart,
Thanks for the idea, but that solves the non-existent problem of the
textbox/numberbox not updating from the listbox's data. They are
updating fine. It's the listbox (in the first code sample) that is
having problems with not updating.

It was intented to solve the existent problem, updating the listbox with the
value of the texbox and numberbox, and was based on the first code sample.

Maybe i misunderstood your question, do you want to persist the value from
the textbox & numberbox to the list when a button is clicked or do you want
something else ?

Greetings
 
N

Noel Weichbrodt

Aha, you are correct. My code was swimming before my eyes, sorry.

I did try your suggestion, and thank you for it. It resulted in the row
under edit being removed from the listbox display, though still showing
up in the right place. In other words, where the listbox would display
three items before the edit, it only displayed two after the edit, with
the row that was edited not shown. However, when I click on the
listbox, the row edited shows up at the proper place (eg if it was at
the top before, it still is, though not displayed), but the listbox
only shows two items, so the now-empty third space will not produce
anything when selected.

Confusing, but the gist is that your code suggestion does not solve the
problem. However, the listbox does show some sort of change, which was
not happening before. Any other suggestions?

TIA,
--Noel
 
B

Bart Mermuys

Hi,
[Inline]

Noel Weichbrodt said:
Aha, you are correct. My code was swimming before my eyes, sorry.

I did try your suggestion, and thank you for it. It resulted in the row
under edit being removed from the listbox display, though still showing
up in the right place. In other words, where the listbox would display
three items before the edit, it only displayed two after the edit, with
the row that was edited not shown. However, when I click on the
listbox, the row edited shows up at the proper place (eg if it was at
the top before, it still is, though not displayed), but the listbox
only shows two items, so the now-empty third space will not produce
anything when selected.

Yes, you're right something odd is going on but only if you use both
directions (list <-> controls). Apperantly calling .EndEdit() causes
SelectedIndexChanged to be fired, what's more SelectedItem is at that point
null and causes a (hidden) exception, because you're trying to use
SelectedItem.

So, keep the first piece of code I gave you and inside the second piece of
code you gave, first check if SelectedItem isn't null :

Private Sub lbxRooms_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Handles lbxRooms.SelectedIndexChanged

If ( Not lbxRooms.SelectedItem Is Nothing ) Then
Dim drv As DataRowView
drv = DirectCast(lbxRooms.SelectedItem, DataRowView)

tbxName.Text = drv.Item("Name").ToString
numSize.Value = CInt(drv.Item("Size"))
End If

End Sub


HTH,
Greetings
 
N

Noel Weichbrodt

Right on. That does the trick. Nice bit of work to figure out that
EndEdit() causes a hidden exception. SelectedItem being null was what I
was running into trying to do this any number of different ways before
I posted, but I couldn't figure out how to diagnose and fix it. Thanks
for your help!

--Noel
 

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