简体   繁体   English

删除组合框中的所选项目

[英]Remove selected item in combobox

I want to remove a selected item in my 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";
    }

Then I have this code on SelectedValueChanged 然后我在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();
    }

I have this code in SelectedIndexChanged 我在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)
            {

            }

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! 提前致谢!

Use a BindingSource for your DataSource and CurrentItemChanged to react of changed items in CBO: 为您的DataSourceCurrentItemChanged使用BindingSource来响应CBO中已更改的项目:

this.source = new BindingSource();
this.source.DataSource = loDs.Tables[0];
this.cmbField.DataSource = this.source;
this.source.CurrentItemChanged += source_CurrentItemChanged;

Example for eventHandler: eventHandler的示例:

private void source_CurrentItemChanged(object sender, EventArgs e)
{
    System.Data.DataRowView view = this.source.Current as System.Data.DataRowView;
    if (view != null)
    {
        System.Diagnostics.Debug.WriteLine(view[0].ToString());
    }
}

You can remove an item from the source like this: 您可以像这样从源中删除一个项目:

this.source.RemoveAt(this.source.Find("FieldName", "PATIENTNAME"));

Show Details: A Detailed Data Binding Tutorial 显示详细信息: 详细的数据绑定教程

You can't modify the Items collection when it comes from / is bound to a DataSource . 当它来自/绑定到DataSource时,不能修改Items集合。 Instead you need to modify the DataSource itself. 相反,您需要修改DataSource本身。

To delete the SelectedItem from the DataSource you can try this: 要从DataSource删除SelectedItem ,可以尝试以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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