Moving item from 1 listbox to another listbox

P

perrycheung221

Hi guys,

I got 2 listboxes in a window form, one on left and one on right. The
1st listbox have some items while the 2nd listbox is empty. Also there
are 2 buttons between the 2 listboxes which used to move item from/to
the 1st and 2nd listbox

My problem here is that after I bind the data to the 1st listbox (from
a dataTable, using DisplayMember and ValueMember), and I try to move 1
of the item from this 1st listbox to the 2nd listbox by:

If ListBox1.SelectedIndex <> -1 Then
ListBox2.Items.Add(ListBox1.SelectedItem)
ListBox1.Items.Remove(ListBox1.SelectedItem)
End If

Item can be add to the 2nd listbox but the displayText of it becoming:

System.Data.DataRowView


Anyone can help? Thanks!! :)
 
C

Cor Ligthert [MVP]

Perry,

The listbox has two methods to show items, one by using his own collection,
second by showing it from the underlying bounded collection.

They can not mix up while the second method has priority. Do you want to
achieve what you want, than you have or all the items first to set in the
listbox items and not bind or you have to move the rows in your datatables.

If it is about one datatable, than you can just add a column which tells in
what listbox it has to be.

Cor
 
P

perrycheung221

Thanks for your reply Cor :)

Yea, I just got one datatable, Would you please tell me the way to
bind the data from a dataTable (which got 2 column: ID and Name. In
this case, the displaymember will be Name for Listbox1, Value will be
ID) ?

Many Thanks
 
C

Cor Ligthert [MVP]

Perry,

Written here so watch typos

dim myFilterColumn as dataColumn(filter)
myDataTable.Columns.Add("myFilterColumn")
dim myDataView as new DataView(myDataTable)
dim myDataView2 as new DataView(myDataTable)
myDataView1.RowFilter = "myFilterColumn = 1"
myDataView1.RowFilter = "myFilterColumn = 2"
myDataView1(3)."myFilterColumn" = "1"
etc.

myListBox1.datasource = myDataView1
myListBox2.datasource = myDataView1


In this situation only the second name will be displayed in listbox1
 

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