Binding a List to Repeater in WinForm application

穿精又带淫゛_ 提交于 2019-12-23 03:23:17

问题


I recently started this question. The suggested approach was to use a DataRepeater.

I've seen a lot of examples on how to bind the repeater, but all of them were for ASP, not Windows Form applications.

I have added Label, PictureBox and Button components to the template, but I've not been able to successfully bind my IList<SomeObject> to my DataRepeater.

I'd like to populate those components with information from a list.

How to bind a IList<SomeObject> to a DatarRepeater in WinForm applications?


回答1:


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

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

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


来源:https://stackoverflow.com/questions/15370914/binding-a-list-to-repeater-in-winform-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!