Binding listbox to a dataview. Please help...

  • Thread starter Thread starter Max
  • Start date Start date
M

Max

I am writing windows application in vb.net and I have scenario where I
have two listboxes side by side and I want to "copy" selected items in
listbox on the left to the one on the right. Both listboxes bound to
two different dataviews like this:
vueToStudents.Table = dsStudents.Tables("Students")
vueToStudents.RowFilter = "SchoolID = 'R39'"
vueToStudents.Sort = "LastName"

listToStudents.DataSource = vueToStudents
listToStudents.DisplayMember = "StudentName"
listToStudents.ValueMember = "StudentID"

Ok now I want to add a row to vueToStudents (eventually rows would
need to come from vueFromStudents which is bounded to a
listFromStudents but to simplify my problem I did the following)

Private Sub btnAddRow(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAddRow.Click
Dim row As DataRowView = vueToStudents.AddNew()
row("SchoolID") = "R39"
row("StudentID") = "123456"
row("FirstName") = "John"
row("LastName") = "Smith"
row.EndEdit()
End Sub

This works kind of like I expect. It adds a row to a listToStudents
(since it's bounded to vueToStudents) but this new row is just a
selected blank row.
Now if I click on a button btnAddRow again it "changes" the blank row
to the one with information above and it adds another blank row.
What do I need to do so that when I click on a button a row is
displayed with information that I added and not as blank? I tried to
rebind listToStudents to vueToStudents right after adding a row but
that didn't help. The correct row is displayed only after I click on a
button for a second time (when the form is reloaded). I'd appreciate
any help to this problem. Thank you.
 
listboxes are made up of listitems so you would just need to add a listitem
object to the destination listbox

DestListBox.Items.Add(new (ListItem(OriginalListBox.SelectedItem.Text,
OriginalListBox.SelectedItem.Value));

that would add the selected item in the first listbox to the second
 
Thank you for your help,
I know how to add item into a listbox but I also need to to update the
database with the items I am adding to that listbox. In other words, I
am adding to the listbox for user to see but behind the scenes the
database should be updated.
So I wanted to add a row to the dataview bounded to this listbox (which
in turn should display item in a listbox). So then I can use data
adapter's Update method to update my database.

Right now I get this behavior:
click on a button once -> row gets added to a dataview and listbox but
it's blank in listbox
click on a button second time -> two rows appear with the information
I've added (correct behavior)

So there is some "late" binding going on here...
 
Ok I found solution to my problem.
After adding a row to a dataview I had to do

dsStudents.Tables("Students").AcceptChanges()
to commit changes made to the datatable.

Although it seems weird because I thought doing row.EndEdit() should
automatically add the row to underlying DataTable.

If anybody needs more info on this check out:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide
/html/cpconmodifyingdatausingdataview.asp
 
Back
Top