我有两个列表框,当我将值交换到另一个列表框时,它总是从列表中删除下一个值,当我尝试从列表中交换最后一个值时,它会从列表中取顶部一个值。我试图找到问题,但我没有得到任何结果。以下是供审查的代码部分。
private void MoveListBoxItems(ListBox lstEmployeelist, ListBox lstSelectedEmployees)
{
ListBox.SelectedObjectCollection sourceItems = lstEmployeelist.SelectedItems;
List<Master> newsource = this.masterBindingSource.DataSource as List<Master>;
List<Master> _selectedSource = this.masterSellectedBindingSource.DataSource as List<Master>;
try
{
if (lstEmployeelist.Items.Count > 0)
{
for (int i = 0; i <= sourceItems.Count -1 ; i++)
{
Master item = sourceItems[i] as Master;
this.masterSellectedBindingSource.AddNew();
Master sitems = masterSellectedBindingSource.Current as Master;
sitems.Empno = item.Empno;
sitems.FirstName = item.FirstName;
newsource.Remove((Master)item);
}
if (sourceItems.Count > 0)
this.masterBindingSource.RemoveCurrent();
this.masterSellectedBindingSource.EndEdit();
lstSelectedEmployees.DataSource = masterSellectedBindingSource;
lstSelectedEmployees.DisplayMember = "FirstName";
lstSelectedEmployees.ValueMember = "Empno";
}
}
catch (Exception ex)
{
throw ex;
}
}