|
-
Nov 11th, 2008, 10:43 AM
#1
Thread Starter
Junior Member
[2008] add items from listbox to another
Hi
I have 2 listboxes,I want to add items from ListBox1 to ListBox2,ListBox1 is populated as follow:
Dim dt As New CustomersTableAdapter
ListBox1.DataSource = dt.GetData()
ListBox1.DisplayMember = "CompanyName"
ListBox1.ValueMember = "CustomerID"
I want to add the selected item from listbox1 to listbox2(the text and the value) so that each item in listbox2 will have a displaymember and a valuemember
any help
thanks
-
Nov 11th, 2008, 11:42 AM
#2
Re: [2008] add items from listbox to another
Try this
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim item As Object
For index As Integer = ListBox1.Items.Count - 1 To 0 Step -1
If ListBox1.GetSelected(index) = True Then
item = ListBox1.Items(index)
ListBox2.Items.Add(item.ToString)
ListBox1.Items.Remove(item) 'this is optional - you dont have to remove them
End If
Next
End Sub
-
Nov 11th, 2008, 12:11 PM
#3
Re: [2008] add items from listbox to another
Or if you don't mind LINQ - give this a shot:
VB Code:
Me.ListBox2.Items.AddRange(From hold In Me.ListBox1.SelectedItems Where not Me.ListBox2.Items.Contains(hold) Distinct.ToArray)
-
Nov 11th, 2008, 10:19 PM
#4
Re: [2008] add items from listbox to another
I'm afraid the previous suggestions won't work because you can't simply shuffle items from one control to the other when they're bound. You have to shuffle items from data source to the other.
In your case, that means taking DataRows from one bound DataTable and adding the to another. If you don't already have the second ListBox bound the you'll need to do that first. Call the Clone method of your DataTable to create an empty copy and bind that to the second ListBox in the same way. You can then import rows from one table into the other:
vb.net Code:
For Each view As DataRowView In Me.ListBox1.SelectedItems table2.ImportRow(view.Row) Next
Note that that will copy the rows into the second table. If you want to move them then that's a little, but not much, more complicated.
-
Nov 12th, 2008, 01:46 AM
#5
Re: [2008] add items from listbox to another
That's opening up new, unpresented doors to their inquiry. Who's to say the datasource is dynamic and will change? Also, aren't you taking the reliance on a UI control (namely listbox) and moving it to another by using dgvs? They're bound to the same limitations and require similiar (if not identical) functionality to keep the control's datasrouce parallel to what's being presented?
-
Nov 12th, 2008, 01:58 AM
#6
Re: [2008] add items from listbox to another
 Originally Posted by syntaxeater
That's opening up new, unpresented doors to their inquiry. Who's to say the datasource is dynamic and will change? Also, aren't you taking the reliance on a UI control (namely listbox) and moving it to another by using dgvs? They're bound to the same limitations and require similiar (if not identical) functionality to keep the control's datasrouce parallel to what's being presented?
I don't really know what you just said. dgvs? DataGridViews? None here. We know, because the OP said so in the first post, that the first ListBox is bound to a DataTable. As such, each item is a DataRowView that corresponds to a DataRow in that DataTable.
Remember that, when you bind a DataTable to a control, it's the contents of its DefaultView, which is a DataView, that actually gets exposed to the control. That's because the DataTable class implements the IListSource interface and its GetList method returns its DefaultView, which is a DataView, which implements the IList interface.
Now, the OP also said that the second ListBox needs to have the same DisplayMember and ValueMember. As such, it must be bound to something. You can't just add items to it directly because then, while you can set a DisplayMember, you cannot set a ValueMember. You don't necessarily have to bind to a DataTable but it seems a bit superfluous to me to define a custom type, extract data from the existing DataRows, create instances of that type, add them to a collection and bind it when you already have the collection and the items available to you. Just basically copy what you already have and bind to it.
-
Nov 12th, 2008, 03:38 AM
#7
Thread Starter
Junior Member
Re: [2008] add items from listbox to another
jmcilhinney I got you point about how to add items from listbox1 to listbox2 by using datatable,but i need to know about how can i remove items from listbox2,is it by simply deleting the datarow from the datatable that is bound
to listbox2?
thanks
-
Nov 12th, 2008, 06:42 AM
#8
Re: [2008] add items from listbox to another
 Originally Posted by cuterita7
jmcilhinney I got you point about how to add items from listbox1 to listbox2 by using datatable,but i need to know about how can i remove items from listbox2,is it by simply deleting the datarow from the datatable that is bound
to listbox2?
thanks
It's not quite that simple. First up, you have to decide whether you want to delete the rows or remove them. There's a difference. Deleting means that the row is still in the table but marked as deleted, so that it can later de deleted from the database too. Removing means actually taking it out of the DataTable as though it was never there. If the row is removed from the DataTable then no changes you made to it can be saved to the database.
You also couldn't use the same loop as I did because by removing a row you will change the SelectedItems in the ListBox, thus negating the rest of the loop. You'd have to copy the selected indices into an array and then loop backwards through that, copying the row first and then deleting or removing it.
-
Nov 12th, 2008, 10:35 AM
#9
Re: [2008] add items from listbox to another
You're correct, I misread your sample (Yes; DGV = datagridview). My mistake.
However, if we used datasource instead of adding range on listbox2; wouldn't we accomplish the same thing?
VB Code:
Me.ListBox2.DataSource = (From hold In Me.ListBox1.SelectedItems).ToList
Since the results are not created until the expression is extracted, this should work out for both adding and removing/deleting?
-
Nov 12th, 2008, 06:37 PM
#10
Re: [2008] add items from listbox to another
 Originally Posted by syntaxeater
You're correct, I misread your sample (Yes; DGV = datagridview). My mistake.
However, if we used datasource instead of adding range on listbox2; wouldn't we accomplish the same thing?
VB Code:
Me.ListBox2.DataSource = (From hold In Me.ListBox1.SelectedItems).ToList
Since the results are not created until the expression is extracted, this should work out for both adding and removing/deleting?
That's quite possibly the case. Haven't really used LINQ myself so can't really advise others on how to do so. If there's no specific need to have a DataTable bound to the other ListBox then it's definitely the most succinct way to accomplish the desired result, once you know exactly how.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|