0

当我在这里看到这篇文章时。

但是在我的代码中我没有看到任何DataBind()方法。

lstBox.DataBind();

如何listbox在 C#.Net 中重新加载?

Refresh()方法也没有奏效。

4

2 回答 2

4

您可以尝试使用ObservableCollection作为 ItemSource,一切都会自动完成。然后,您的任务是将项目填充到 ObservableCollection 中,无需手动更新。

于 2013-03-15T06:19:10.410 回答
2

DataBind() 用于 ASP.NET 控件 - 据我所知,Windows 窗体控件没有等效的方法。您的列表框的数据源是什么?我记得不久前遇到过类似的问题,我通过将控件绑定到 BindingSource 对象而不是我使用的任何对象来解决我的问题。同样,将 Listbox 绑定到 BindingSource 而不是当前数据源可能对您有利。来自MSDN

BindingSource 组件有多种用途。首先,它通过在 Windows 窗体控件和数据源之间提供货币管理、更改通知和其他服务来简化窗体上的控件与数据的绑定。

换句话说,一旦您对 BindingSource 进行了更改(例如调用 BindingSource.Add,或将 BindingSource 的 DataSource 属性设置为另一个集合),您的 ListBox 将自动更新,而无需调用类似“DataBind()”的方法.

如果您的列表框当前绑定到集合对象,您可以简单地将集合绑定到 BindingSource,然后将控件绑定到 BindingSource:

BindingSource.DataSource = ListboxItems;
ListBox.DataSource = BindingSource;

或者,您可以手动构建 BindingSource 的内容:

MyBindingSource.Clear();
MyBindingSource.Add(new BusinessObject("Bill", "Clinton", 1946));
MyBindingSource.Add(new BusinessObject("George", "Bush", 1946));
MyBindingSource.Add(new BusinessObject("Barack", "Obama", 1961));

lstBox.DataSource = MyBindingSource;
于 2013-03-15T06:51:30.717 回答