我最近started this question。建议的方法是使用DataRepeater
。
我已经看过很多关于如何绑定转发器的例子,但它们都是针对ASP的,而不是Windows Form应用程序。
我已将Label
,PictureBox
和Button
个组件添加到模板中,但我无法将IList<SomeObject>
成功绑定到DataRepeater
}。
我想用列表中的信息填充这些组件。
如何在WinForm应用程序中将IList<SomeObject>
绑定到DatarRepeater
?
答案 0 :(得分:1)
终于搞定了!为了将来参考,这是我使用的:
首先使用BindingSource
:
private BindingSource bindingSource ;
private void InitUserListArea()
{
_bindingSource = new BindingSource();
_bindingSource.DataSource = tempUsers;
_labelUserListRoleValue.DataBindings.Add("Text", _bindingSource, "Role");
_labelUserListNameValue.DataBindings.Add("Text", _bindingSource, "Name");
_labelUserListLastAccessValue.DataBindings.Add("Text", _bindingSource, "LastAccess");
_dataRepeaterUserList.DataSource = _bindingSource;
}
然后获取数据(在我的例子中来自webservice)并用数据填充列表。填充列表后,或发生任何更改时:
private void RefreshDataRepeater()
{
if (_dataRepeaterUserList.InvokeRequired)
{
_dataRepeaterUserList.Invoke((Action)(() => { RefreshDataRepeater(); }));
return;
}
_bindingSource.DataSource = null;
_bindingSource.DataSource = tempUsers;
_dataRepeaterUserList.DataSource = null;
_dataRepeaterUserList.DataSource = _bindingSource;
_dataRepeaterUserList.Refresh();
}