简体   繁体   中英

Exclude Combo box items that held in a datagridview

I have a combo box that is attached to a datasource

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

I also have a datagridView that has a list of selections that have selected from the combo box .

I am trying to remove the items for the combo box if they have already have the item on the Datagridview or warn the user that it is already selected.

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

If you are using a data source then you should not be interacting with the Items collection. The MSDN documentation says :

A data source can be a database, a Web service, or an object that can later be used to generate data-bound controls. When the DataSource property is set, the items collection cannot be modified .

Instead, you should be managing your own collection using a BindingList .

Example in C# (sorry):

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM