删除组合框中的所选项目 [英] Remove selected item in combobox

查看:164
本文介绍了删除组合框中的所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要删除我的组合框中的所选项

I want to remove a selected item in my combobox

我在表单加载时在这里有一个代码,我从数据库填充combobox上的列表项。

I have here a code upon form load, I am filling list items on combobox from database.

private void LoadComboField()
    {
        //string test = "<ROOT><DATA FieldGroup=\"PAYMENT_VIEW4\" FieldDescription=\"PNAME\" Output=\"1\" Filter=\"1\" FieldName=\"PATIENTNAME\" DataType=\"STRING\"/><DATA FieldGroup=\"PAYMENT_VIEW4\" FieldDescription=\"MEMID\" Output=\"1\" Filter=\"1\" FieldName=\"MEMBERID\" DataType=\"STRING\"/></ROOT>";
       ReadXMLData(XMLDOC, dsCombo);
       // ReadXMLData(test, dsCombo);
        dt = dsCombo.Tables[0];
        DataView dv1 = new DataView(dsCombo.Tables[0]);
        this.cmbField.Items.Clear();
        this.cmbField.DataSource = dv1;
        this.cmbField.DisplayMember = "FieldDescription";
        this.cmbField.ValueMember = "FieldName";
    }

然后我在SelectedValueChanged上有这个代码

Then I have this code on SelectedValueChanged

private void cmbField_SelectedValueChanged(object sender, EventArgs e)
    {
        DataGridViewRow GridRowLoc = this.dgvFilter.CurrentRow;
        AddGrid(iRowIdx);
        int iRowCount = this.dgvFilter.RowCount - 1;
        //this.dgvFilter.CurrentRow.IsNewRow
        //if (GridRowLoc.IsNewRow) continue;


       // MessageBox.Show(this.dgvFilter.RowCount.ToString());
        if (this.cmbField.Text != "System.Data.DataRowView")
        {

            this.dgvFilter.Rows[iRowIdx].Cells["ColumnFieldName"].Value = this.cmbField.Text;
            this.dgvFilter.Rows[iRowIdx].Cells["FieldName"].Value = this.cmbField.SelectedValue;


            if (iRowCount <= iRowIdx)
            {
                DataRow drow = dttable.NewRow();
                drow["ColumnNames"] = this.cmbField.Text;
                drow["FieldName"]= this.cmbField.SelectedValue;
                drow["Alias"]=string.Empty;
                drow["DataType"]=string.Empty;
                drow["Outputs"]=false;
                drow["SortType"]=string.Empty;
                drow["SortOrder"]=string.Empty;
                drow["GroupBy"]=string.Empty;
                drow["Filter"]=string.Empty;
                drow["Or1"]=string.Empty;
                drow["Or2"]=string.Empty;
                drow["Or3"]=string.Empty;
                drow["Or4"]=string.Empty;
                drow["Or5"]=string.Empty;
                drow["Or6"]=string.Empty;
                drow["Or7"]=string.Empty;
                drow["Or8"]=string.Empty;
                drow["Or9"]=string.Empty;
                drow["Or10"]=string.Empty;
                dttable.Rows.Add(drow);
            }
            else
            {
                int irow = 0;
                foreach (DataRow dr in dttable.Rows)
                {
                    if (irow == iRowIdx)
                    {
                         dr["ColumnNames"] = this.cmbField.Text;
                         dr["FieldName"] = this.cmbField.SelectedValue;
                    }
                    irow++;                     
                }
            }

            CheckAlias(iRowIdx, this.cmbField.Text, dgvFilter);
            checkcellvalue(this.cmbField.Text, iRowIdx);
            CheckSorting();
            if (bGroupBySelected == true)
            {
                this.dgvFilter.Rows[iRowIdx].Cells["GroupBy"].Value = "Group By";
            }

            this.dgvFilter.DataSource = dttable;
            dsFilter.AcceptChanges();

            this.cmbField.Visible = false;
         }
       // checkcellvalue(this.cmbField.Text, iRowIdx);
        //MessageBox.Show(arr_Filter[0]);

        CheckoutputEnable();
    }

我在SelectedIndexChanged中有此代码

I have this code in SelectedIndexChanged

  try
            {
                DataTable dt1 = new DataTable();
                DataRowView oDataRowView = cmbField.SelectedItem as DataRowView;
                string sValue = string.Empty;

                if (oDataRowView != null)
                {
                    sValue = oDataRowView.Row["FieldDescription"] as string;
                }
                //int count = dttable.Rows.Count - 1;
                ComboBox comboBox = (ComboBox)sender;

                // Save the selected employee's name, because we will remove 
                // the employee's name from the list. 
                string selectedEmployee = (string)sValue;

                int count = 0;
                int resultIndex = -1;

                // Call the FindStringExact method to find the first  
                // occurrence in the list.
                resultIndex = cmbField.FindStringExact(selectedEmployee);

                // Remove the name as it is found, and increment the found count.  
                // Then call the FindStringExact method again, passing in the  
                // index of the current found item so the search starts there  
                // instead of at the beginning of the list. 
                while (resultIndex != -1)
                {
                    cmbField.Items.RemoveAt(resultIndex);
                    count += 1;
                    resultIndex = cmbField.FindStringExact(selectedEmployee,
                        resultIndex);
                }
                // Update the text in Textbox1.
                txtName.Text = txtName.Text + "\r\n" + selectedEmployee + ": "
                    + count;
            }
                //}
            catch (Exception ex)
            {

            }

但它抛出一个异常,说当设置了datasource属性时,不能修改items集合。我不知道如何解决这个异常错误,我认为这是我唯一的问题,当删除组合框上的项目。

But it throws an exception, say that "items collection cannot be modified when the datasource property is set." I don't know how to fix this exception error, I think that's my only problem when removing an item on the combobox.

请帮助我在这一个。提前感谢!

Please do help me on this one. Thanks in advance!

推荐答案

您无法修改 Items 当它来自/被绑定到一个 DataSource 。您需要修改 DataSource 本身。

You can't modify the Items collection when it comes from / is bound to a DataSource. Instead you need to modify the DataSource itself.

要删除 SelectedItem DataSource 可以尝试:

 DataRowView item = cmbField.SelectedItem as DataRowView;
 if (item != null) item.Delete();

这篇关于删除组合框中的所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆