如何在C#WinForms中刷新ListBox的DataSource

表单有一个combobox和一个ListBox。 当单击“添加”button时,我想将所选项目从combobox添加到列表框。

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(); } } 

在这个例子中,select的项目被replace为ListBox内的新select。 我需要将该项目添加到列表中。

我的代码有什么问题?

谢谢。

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); } } 

我会build议使用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); } } 

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

列表框没有检测到你已经改变了DataSource 。 它只会在Datasource发生变化时刷新,因此首先将DataSource设置为null:

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

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

 listBox1.Items.Clear(); listBox1.DataSource = data; 

或者,也许最正确的方法来实现这是使用提供的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。

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

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

ShowData()方法,其中BindingSourceDisplayMemberValueMember被设置

 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(); } }