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中的新选择。我需要将项目添加到列表中。
我的代码出了什么问题?
答案 0 :(得分:64)
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);
}
}
答案 1 :(得分:26)
我建议使用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);
}
}
在飞行中更改控件数据源有时会产生奇怪的结果。
答案 2 :(得分:16)
列表框未检测到您更改了DataSource
。它只会在Datasource
发生更改时刷新,因此请先将DataSource
设置为null:
listBox1.DataSource = null;
listBox1.DataSource = data;
您也可以清除这些项目,然后再次设置DataSource:
listBox1.Items.Clear();
listBox1.DataSource = data;
答案 3 :(得分:0)
或者,可能最正确的方法是使用提供的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。
答案 4 :(得分:0)
在表单初始化时调用ShowData()
以在初始化
public Department()
{
InitializeComponent();
ShowData();
}
ShowData()
方法,设置BindingSource
,DisplayMember
和ValueMember
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();
}
}
答案 5 :(得分:0)
也许此解决方案没有更好的性能,但是经过多次尝试和几个小时,它才对我有用:
此行是在表单构造函数上执行的:
listBox1.DataSource = myData;
This lines were executed after the information was modified:
listBox1.DataSource = new List<Movil>();
listBox1.DataSource = myData;
希望有帮助!
答案 6 :(得分:0)
刷新也可以通过
进行listbox.ItemsSource = null;
listbox.ItemsSource = data;