listBox2.DataSource = listBox1.SelectedItems;
listBox2.DisplayMember = "Descripcion";
listBox2.ValueMember = "Id";
使用上述代码后,我无法一一选择。帮助!有人请发布代码以删除
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
使用 SqlDataAdapter 将数据从数据源转储到列表框中,并根据其中一个列表框使用 listbox.add() 方法中选择的数据,使用“for”循环使用索引将数据从一个列表框转储到另一个列表框。
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";
这工作正常....