简体   繁体   中英

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

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

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());
}
    if (listBox1.SelectedItem != null)
        {
            listBox2.Items.Add(listBox1.SelectedItem);
        }
        listBox2.DisplayMember = "Descripcion";
        listBox2.ValueMember = "Id";

this is working fine....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM