【问题标题】:Exclude Combo box items that held in a datagridview排除保存在 datagridview 中的组合框项目
【发布时间】:2014-11-13 21:26:17
【问题描述】:

我有一个combo box 附加到datasource

cboPies.DataSource = GetPies(txtCustomer.Text)
cboPies.DisplayMember = "PIES_DESCN"
cboPies.ValueMember = "PIES_ID"

我还有一个datagridView,其中包含从combo box 中选择的选项列表。

如果combo box 上已有项目,我将尝试删除combo box 的项目,或者警告用户它已被选中。

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

【问题讨论】:

  • 运行代码时是否收到错误消息?
  • 不,它没有显示错误,但似乎没有删除数据网格视图中已经存在的任何项目。

标签: vb.net winforms visual-studio-2010 datagridview combobox


【解决方案1】:

如果您使用的是数据源,则不应与 Items 集合进行交互。 The MSDN documentation says:

数据源可以是数据库、Web 服务或对象 稍后用于生成数据绑定控件。当数据源 属性已设置,无法修改项目集合

相反,您应该使用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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    相关资源
    最近更新 更多