我有combo box
附加到datasource
cboPies.DataSource = GetPies(txtCustomer.Text)
cboPies.DisplayMember = "PIES_DESCN"
cboPies.ValueMember = "PIES_ID"
我还有一个datagridView
,其中包含从combo box
中选择的选项列表。
我正在尝试删除combo box
的项目,如果他们已经拥有Datagridview
上的项目或警告用户已经选择了该项目。
With dgvSelectedPies
For indexDGV As Integer = 0 To .Rows.Count - 1 Step 1
'cboSpecialty.Items.Remove(.Rows(indexDGV).Cells("PIES_DESCN").Value)
cboSpecialty.Items.Remove(.Rows(indexDGV).Cells("PIES_ID").Value)
Next
End With
答案 0 :(得分:1)
如果您使用的是数据源,则不应与Items
集合进行交互。 The MSDN documentation says:
数据源可以是数据库,Web服务或可以的对象 稍后用于生成数据绑定控件。当DataSource 已设置属性,无法修改项目集合。
相反,您应该使用BindingList来管理自己的收藏。
C#中的示例(抱歉):
protected BindingList<Pies> ComboDataSource { get; set; }
...
ComboDataSource = new BindingList<Pies>(GetPies(txtCustomer.Text));
cboPies.DataSource = ComboDataSource;
cboPies.DisplayMember = "PIES_DESCN"
cboPies.ValueMember = "PIES_ID"
...
if(ComboDataSource.Contains(pieInDataGrid))
{
ComboDataSource.Remove(pieInDataGrid);
}