【问题标题】:Remove selected item in combobox删除组合框中的选定项目
【发布时间】:2015-04-08 21:51:07
【问题描述】:

我想删除组合框中的选定项目

我在这里有一个加载表单的代码,我正在从数据库中填充组合框中的列表项。

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 上有这段代码

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 中有这段代码

  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)
            {

            }

但它会抛出一个异常,说“设置数据源属性时无法修改项目集合”。我不知道如何解决这个异常错误,我认为这是我在删除组合框上的项目时唯一的问题。

请帮我解决这个问题。提前致谢!

【问题讨论】:

  • 什么是数据源,您是否尝试将其从那里删除?
  • @Pranav-BitWiser 数据源是这个DataView dv1 = new DataView(dsCombo.Tables[0]);在表单加载时,它在上面的问题中私有 void LoadComboField,如果我删除它,我的组合框中将没有项目。
  • Pravnav 是对的:如果 LB 是数据绑定的,您需要删除的项目不是从 LB,而是 数据源。不要删除 数据源,只删除 中的项目!
  • @TaW 我没听懂你在说什么,你能发布一个示例答案吗?

标签: c# combobox


【解决方案1】:

为您的DataSourceCurrentItemChanged 使用BindingSource 以响应CBO 中更改的项目:

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

事件处理程序示例:

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

您可以像这样从源中删除项目:

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

显示详情:A Detailed Data Binding Tutorial

【讨论】:

  • 我试过你的代码,但我的组合框中的项目变成了 System.Data.DataRowView
  • this.source 是一个 BindingSource。实际上,组合框项目是 DataRowView。对于添加、删除、更新、过滤、排序等,您必须仅使用 BindingSource ...而不是组合框中的项目。
【解决方案2】:

Items集合来自/绑定到DataSource时,您不能修改它。相反,您需要修改 DataSource 本身。

要从DataSource 中删除SelectedItem,您可以试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多