Paul
Paul

Reputation: 39

How to select items and move from one list box with datasource to another?

listBox2.DataSource = listBox1.SelectedItems;
listBox2.DisplayMember = "Descripcion";
listBox2.ValueMember = "Id";

After using the above code, I am not able to select one by one. Help! some one please post codes to remove too

Upvotes: 0

Views: 1695

Answers (3)

Dilip Kumar.S
Dilip Kumar.S

Reputation: 1

Use SqlDataAdapter to dump data into the listbox from the data source and based on the data selected in one of the listbox use listbox.add() method, Use a "for" loop to dump the data from one listbox to another using an index.

for(int i=0; i<listBox1.SelectedItems.Count; i++)
{
    listBox2.Items.Add(listbox1.SelectedItems[i].toString());
}

Upvotes: 0

Paul
Paul

Reputation: 39

    if (listBox1.SelectedItem != null)
        {
            listBox2.Items.Add(listBox1.SelectedItem);
        }
        listBox2.DisplayMember = "Descripcion";
        listBox2.ValueMember = "Id";

this is working fine....

Upvotes: 0

Joffrey Kern
Joffrey Kern

Reputation: 6499

First, you have to define a model for your Listbox :

public class MyModel
{
    public int Id { get; set; }
    public string Description { get; set; }
}

After you can set items like this :

listBox2.Items.Add(new MyModel() { Id = 1, Description = "description" });
listBox2.DisplayMember = "Description";
listBox2.ValueMember = "Id";

And now, your listbox will show the description property. If you select an item, the SelectedValue in listbox2 will be the value of id property

Upvotes: 1

Related Questions