且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

绑定列表在WinForm应用程序中继器

更新时间:2022-06-18 03:16:55

终于得到了它的工作!对于未来的参考,这是我用什么:

Finally got it working! For future reference, this is what I used:

首先调用此方法,用来初始化手工绑定,使用的BindingSource

First call this method to initilize manual binding, using a 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;
}

然后获取数据(在我的情况下,从一个Web服务),并用数据填充列表。列表之后被填充时,或当任何改变occurr:

Then get data (in my case from a webservice) and fill the list with data. After the list is populated, or when any changes occurr:

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