Results 1 to 10 of 10

Thread: [2008] add items from listbox to another

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2008
    Posts
    21

    [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

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [2008] add items from listbox to another

    Try this
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim item As Object
    3.         For index As Integer = ListBox1.Items.Count - 1 To 0 Step -1
    4.             If ListBox1.GetSelected(index) = True Then
    5.                 item = ListBox1.Items(index)
    6.                 ListBox2.Items.Add(item.ToString)
    7.                 ListBox1.Items.Remove(item)  'this is optional - you dont have to remove them
    8.             End If
    9.         Next
    10. End Sub

  3. #3
    Hyperactive Member syntaxeater's Avatar
    Join Date
    Dec 2006
    Location
    Des Moines, IA
    Posts
    460

    Re: [2008] add items from listbox to another

    Or if you don't mind LINQ - give this a shot:
    VB Code:
    1. Me.ListBox2.Items.AddRange(From hold In Me.ListBox1.SelectedItems Where not Me.ListBox2.Items.Contains(hold) Distinct.ToArray)

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. For Each view As DataRowView In Me.ListBox1.SelectedItems
    2.     table2.ImportRow(view.Row)
    3. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Hyperactive Member syntaxeater's Avatar
    Join Date
    Dec 2006
    Location
    Des Moines, IA
    Posts
    460

    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?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] add items from listbox to another

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Sep 2008
    Posts
    21

    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

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] add items from listbox to another

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Hyperactive Member syntaxeater's Avatar
    Join Date
    Dec 2006
    Location
    Des Moines, IA
    Posts
    460

    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:
    1. 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?

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] add items from listbox to another

    Quote 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:
    1. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width