53

Form 有一个 Combobox 和一个 ListBox。单击“添加”按钮时,我想将所选项目从 ComboBox 添加到 ListBox。

public partial class MyForm:Form
{
    List<MyData> data = new List<MyData>();
    private void ShowData()
    {
       listBox1.DataSource = data;
       listBox1.DisplayMember = "Name";
       listBox1.ValueMember = "Id";
    }

    private void buttonAddData_Click(object sender, EventArgs e)
    {
       var selection = (MyData)comboBox1.SelectedItem;
       data.Add(selection);
       ShowData();
    }
}

在此示例中,所选项目将替换为 ListBox 内的新选择。我需要将该项目添加到列表中。

我的代码有什么问题?

4

7 回答 7

80

listbox1.DataSource属性查找值更改,但通过始终分配相同的列表,值不会真正改变。

您可以使用BindingList<T>代替您的List<T>来自动识别添加的新项目。您的 ShowData() 方法必须在启动时调用一次。

public partial class MyForm:Form
{
    public MyForm(){
        InitializeComponent();
        ShowData();
    }

    BindingList<MyData> data = new BindingList<MyData>();

    private void ShowData()
    {
       listBox1.DataSource = data;
       listBox1.DisplayMember = "Name";
       listBox1.ValueMember = "Id";
    }

    private void buttonAddData_Click(object sender, EventArgs e)
    {
       var selection = (MyData)comboBox1.SelectedItem;
       data.Add(selection);
    }
}
于 2013-07-12T12:51:11.900 回答
36

我建议使用BindingSource它,因为它会正确更新连接的控件。

public partial class MyForm : Form
{
    List<MyData> data = new List<MyData>();
    BindingSource bs = new BindingSource();

    public MyForm()
    {
        IntializeComponents();
        bs.DataSource = data;

       listBox1.DisplayMember = "Name";
       listBox1.ValueMember = "Id";
       listBox1.DataSource = bs;
    }

    private void buttonAddData_Click(object sender, EventArgs e)
    {
       var selection = (MyData)comboBox1.SelectedItem;
       data.Add(selection);

       bs.ResetBindings(false);
    }
}

动态更改控件数据源有时会产生奇怪的结果。

于 2013-07-12T12:42:00.903 回答
19

列表框未检测到您已更改DataSource. 它只会在Datasource更改时刷新,因此DataSource首先设置为null:

listBox1.DataSource = null;
listBox1.DataSource = data;

您还可以清除项目然后再次设置数据源:

listBox1.Items.Clear();
listBox1.DataSource = data;
于 2013-07-12T12:50:59.403 回答
1

或者,可能最正确的实现方式是使用提供的ObservableCollection<T>. 它的设计目的是实现INotifyCollectionChanged.

public partial class MyForm : Form
{
    ObservableCollection<MyData> data = new ObservableCollection<MyData>();

    public MyForm()
    {
        listBox1.DataSource = data;
        listBox1.DisplayMember = "Name";
        listBox1.ValueMember = "Id";
    }

    private void buttonAddData_Click(object sender, EventArgs e)
    {
       var selection = (MyData)comboBox1.SelectedItem;
       data.Add(selection);
    }
}

因为ObservableCollection<T>实现INotifyCollectionChanged了 DataSource 绑定,只要您的数据发生变化,就会自动更新 ListBox。

于 2014-07-10T06:09:55.263 回答
0

在表单初始化时调用ShowData()以在初始化时填充您的列表框

 public Department()
        {
            InitializeComponent();
            ShowData();
        }

ShowData()方法、BindingSource位置DisplayMemberValueMember设置

private void ShowData()
            {
                using (var uow = new UnitOfWork(new SellContext()))
                {
                    listBox1.DataSource = uow.Departments.GetAll().ToList();
                    listBox1.DisplayMember = "DepartmentName";
                    listBox1.ValueMember = "DepartmentId"; 
                    //listBox1.Invalidate();       
                }
            }

在下面的实现中,当从数据库中删除部门时,列表框会使用当前集合刷新

private void button1_Click(object sender, EventArgs e)
    {
        try {
            using (var uow = new UnitOfWork(new SellContext()))
            {
                int count = uow.Departments.FindDepartmentByName(txtDeptName.Text.ToString());
                if (count>0)
                {
                    MessageBox.Show("This Department Already Exists", "SellRight", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }

                else
                {
                    department dept = new department();
                    dept.DepartmentName = txtDeptName.Text.ToString();
                    uow.Departments.Create(dept);
                    if (uow.Complete() > 0)
                    {           
                        MessageBox.Show("New Department Created", "SellRight", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txtDeptName.Text = "";
                        ShowData();      
                    }
                    else
                    {
                        MessageBox.Show("Unable to add Department", "SellRight", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txtDeptName.Text = "";
                        ShowData();
                    }
                }
            }                              
        }
        catch(Exception ex)
        {
            ex.ToString();
        }
    }
于 2017-03-17T07:06:47.363 回答
0

刷新也可以通过

listbox.ItemsSource = null;
listbox.ItemsSource = data;
于 2020-10-02T14:09:41.870 回答
0

也许这个解决方案没有更好的性能,但经过多次尝试和几个小时后,它对我有用:

此行在表单构造函数上执行:

listBox1.DataSource = myData;

This lines were executed after the information was modified:
listBox1.DataSource = new List<Movil>();
listBox1.DataSource = myData;

希望能帮助到你!

于 2019-05-05T22:35:25.183 回答