CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2007
    Posts
    84

    copy listbox item to listbox2

    hi,
    i want to copy items from my listbox1 to listbox2

    here is my code
    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                string objSelected = "";
    
                foreach (object sel in listBox1.SelectedItems)
                {
    
                    objSelected = lstBox1.SelectedItems[3].ToString();
                    listBox2.Items.Add(objSelected.ToString());
                }
                
            }
    and my error is always it sees the item selected as datarowview
    Code:
    System.Data.DataRowView
    any advice

    thanks!

  2. #2
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: copy listbox item to listbox2

    You want to copy all of the items from listbox1 to listbox2? In that case, this is the code you should use:

    Code:
    private void button1_Click(object sender, EventArgs e)
    {
        //Use this to copy all items
        foreach (object item in listBox1.Items)
        {
            listBox2.Items.Add(item);
        }
    
        //Use this to copy selected items
        foreach (object item in listBox1.SelectedItems)
        {
            listBox2.Items.Add(item);
        }
    }
    It's not a bug, it's a feature!

  3. #3
    Join Date
    Oct 2007
    Posts
    84

    Re: copy listbox item to listbox2

    sorry, i didnt indicate that i want to copy selected items to another listbox

    tia

  4. #4
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: copy listbox item to listbox2

    Well then use the second loop in my last post
    It's not a bug, it's a feature!

  5. #5
    Join Date
    Oct 2007
    Posts
    84

    Re: copy listbox item to listbox2

    but it only outputs System.data.datarowview and not the items itself.
    btw, my listview has datasource from a table.
    i dont know why it keeps on returning System.data.datarowview.

  6. #6
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: copy listbox item to listbox2

    So... is it a ListBox or a ListView, as it seemed to change after Foamy's post?
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

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

Featured