我有两个列表框。当我将值从一个列表框交换到另一个列表框时,出现异常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";
我该如何解决这个问题?