siddharth
siddharth

Reputation: 451

copying from one list box to other listbox

I am working with windows form application using c#. I have two list boxes i.e listbox1 and listbox2 and one button i.e btnall. listbox1 is bind using databind and listbox2 is empty. I want to copy all the items from listbox1 which is binded to other listbox2 which is empty. On the click of btnAll_click event. I am trying this

private void btnAll_Click(object sender, EventArgs e)
{
    listbox2.Items.AddRange(listbox1.Items);
}

but I am getting data.datarowview instead of the values.

Upvotes: 2

Views: 3812

Answers (3)

Ria
Ria

Reputation: 10367

If you are populating the list control via the DataSource property try setting DataSource and DataMember. and dont forget :

listBox.DisplayMember = "displayMember";
listBox.ValueMember = "valueMember";

else try this:

var mylistSource = new List<string>();
foreach (var item in Listbox1.Items)
{
    mylistSource.Add(item.ToString());
}

listBox2.DataSource = mylistSource;

Upvotes: 0

Gaz Winter
Gaz Winter

Reputation: 2989

You could loop through the items in LIstbox1 and add them one at a time like this:

 foreach (var item in Listbox1.Items)
 {
     listbox2.Items.Add(item.ToString());
 }

Would that do what you want?

Upvotes: 3

JohnnBlade
JohnnBlade

Reputation: 4327

Try this

listbox2.DataSource = listbox1.Items;

Upvotes: 0

Related Questions