0

私は2つのリストボックスを持っています。あるリストボックスから別のリストボックスに値を交換すると、例外が発生しますItems collection cannot be modified when the DataSource property is set.。どうすれば解決できますか?

リストボックスをバインドして値を交換するコードの下

値を交換するには、これを使用します:

 private void MoveListBoxItems(ListBox lstEmployeelist, ListBox lstSelectedEmployees)
    {

        ListBox.SelectedObjectCollection sourceItems = lstEmployeelist.SelectedItems;


        try
        {
            for (int i = 0; i <= sourceItems.Count - 1; i++ )
            {
                object item = sourceItems[i];

                lstSelectedEmployees.Items.Add(item);
                lstEmployeelist.Items.RemoveAt(i);

            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

リストボックスをバインドするには、このコードを使用します

if (_empComponent == null)
            _empComponent = new EmployeeComponent();
        lstEmployeelist.DataSource = _empComponent.GetEmpCodeWithName();
        lstEmployeelist.ValueMember = "Empno";
        lstEmployeelist.DisplayMember = "FirstName";

どうすればこれを解決できますか?

4

2 に答える 2