ComboBox 值不断添加到列表中

ComboBox Values Keeps getting added to the list

我有一个带有两个组合框的表单。

combobox1 unitupc

combobox2生产线

首先,加载 unitupc,然后为每个选择的 unitupc 填充 combobox2。我遇到的问题是,对于用户选择的每个 unitupc,以前的值存储在 combobox2 中并且列表不断添加,每次选择 unitupc 时如何清除组合框并将其重新加载?

这是我正在谈论的问题的表格图片:

已添加编辑代码

  private void DimensionSelection_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'CorsicanaNetWeightDataSet10.Net_Weight_Master_Data_Report' table. You can move, or remove it, as needed.

            // TODO: This line of code loads data into the 'corsicanaNetWeightDataSet9.ProductionLine' table. You can move, or remove it, as needed.

            prodline = new productweightdataset();
            // TODO: This line of code loads data into the 'corsicanaNetWeightDataSet3.ProductionLine' table. You can move, or remove it, as needed.
            //this.productionLineTableAdapter.Fill(this.corsicanaNetWeightDataSet3.ProductionLine,comboBox3.Text.ToString());
            // TODO: This line of code loads data into the 'corsicanaNetWeightDataSet2.ItemDescription' table. You can move, or remove it, as needed.
            //this.itemDescriptionTableAdapter.Fill(this.corsicanaNetWeightDataSet2.ItemDescription);

            loadprod(); 

            this.reportViewer1.RefreshReport();
            reportViewer1.Visible = false;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (comboBox1.SelectedIndex > -1)
            //{
            //    button1.Enabled = true;
            //}

            Loadproduction();
            comboBox2.Refresh();
        }

        private void loadprod()
        {
            try
            {
                using (MSSQL.SqlConnection connection = new MSSQL.SqlConnection(constr))
                {
                    connection.Open();
                    using (MSSQL.SqlCommand command = new MSSQL.SqlCommand("SELECT [Unit UPC Base Item] as Unitupc, [Item Description] AS ItemDescription FROM ItemDesc", connection))
                    {
                        {
                            MSSQL.SqlDataAdapter myadapter = new System.Data.SqlClient.SqlDataAdapter();
                            myadapter.SelectCommand = command;
                            myadapter.Fill(prodline, "DataTable1");

                        }

                    }

                    //fill drop down
                    comboBox1.DataSource = prodline.DataTable1;
                    comboBox1.ValueMember = "ItemDescription";
                    comboBox1.DisplayMember = "ItemDescription";
                    comboBox3.DataSource = prodline.DataTable1;
                    comboBox3.ValueMember = "Unitupc";
                    comboBox3.DisplayMember = "Unitupc";
                    if (comboBox1.Items.Count > 0)
                    {

                        comboBox1.SelectedIndex = 0;
                        Loadproduction();
                    }

                }
            }
            catch (Exception) { /*Handle error*/ }


        }



        //private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    this.productionLineTableAdapter1.Fill(this.corsicanaNetWeightDataSet9.ProductionLine, comboBox3.Text.ToString());
        //}

        private void Loadproduction()
        {
           if (comboBox1.SelectedValue.ToString().Trim().Length > 0)
            {
                try
                {
                    using (MSSQL.SqlConnection connection = new MSSQL.SqlConnection(constr))
                    {
                        connection.Open();

                        using (MSSQL.SqlCommand command = new MSSQL.SqlCommand("SELECT TOP (100) PERCENT dbo.[Production Lines].[Production Line Description] AS prodline FROM dbo.[Production Lines] LEFT OUTER JOIN dbo.[Net Weight Master Data] ON dbo.[Production Lines].[Production Line] = dbo.[Net Weight Master Data].[Production Line] RIGHT OUTER JOIN dbo.ItemDesc ON dbo.[Net Weight Master Data].[Unit UPC Base Item] = dbo.ItemDesc.[Unit UPC Base Item] WHERE (dbo.ItemDesc.[Unit UPC Base Item] = '0001') GROUP BY dbo.[Production Lines].[Production Line], dbo.[Production Lines].[Production Line Description], dbo.ItemDesc.[Unit UPC Base Item] ORDER BY dbo.ItemDesc.[Unit UPC Base Item]", connection))
                        {
                            MSSQL.SqlParameter myparam = new MSSQL.SqlParameter();
                            myparam.Direction = ParameterDirection.Input;
                            myparam.ParameterName = "@unitupc";
                            myparam.SqlDbType = SqlDbType.NVarChar;
                            myparam.Size = 50;
                            myparam.Value = comboBox3.Text;
                            command.Parameters.Add(myparam);
                            MSSQL.SqlDataAdapter myadapter = new System.Data.SqlClient.SqlDataAdapter();
                            myadapter.SelectCommand = command;
                            myadapter.Fill(prodline, "DataTable2");
                            comboBox2.DataSource = prodline.DataTable2;
                            comboBox2.DisplayMember = "prodline";


                        }
                    }
                }
                catch (Exception) { /*Handle error*/ }
            }
        }

你试过了吗:ComboBox2.Items.Clear()?

编辑:我会接受 prasy 的回答。查看 this question 了解更多信息。

在绑定新值之前,您必须清除组合框的数据源。按照建议使用 Clear 将 DataTable 清除为@LarsTech。查看我的编辑

  private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  {         
        comboBox2.DataSource = null;
        Loadproduction();
        comboBox2.Refresh();
  }

编辑:

 private void Loadproduction()
 {
      if (comboBox1.SelectedValue.ToString().Trim().Length > 0)
        {
            try
            {
                using (MSSQL.SqlConnection connection = new MSSQL.SqlConnection(constr))
                {
                    connection.Open();

                    using (MSSQL.SqlCommand command = new MSSQL.SqlCommand("SELECT TOP (100) PERCENT dbo.[Production Lines].[Production Line Description] AS prodline FROM dbo.[Production Lines] LEFT OUTER JOIN dbo.[Net Weight Master Data] ON dbo.[Production Lines].[Production Line] = dbo.[Net Weight Master Data].[Production Line] RIGHT OUTER JOIN dbo.ItemDesc ON dbo.[Net Weight Master Data].[Unit UPC Base Item] = dbo.ItemDesc.[Unit UPC Base Item] WHERE (dbo.ItemDesc.[Unit UPC Base Item] = '0001') GROUP BY dbo.[Production Lines].[Production Line], dbo.[Production Lines].[Production Line Description], dbo.ItemDesc.[Unit UPC Base Item] ORDER BY dbo.ItemDesc.[Unit UPC Base Item]", connection))
                    {
                        MSSQL.SqlParameter myparam = new MSSQL.SqlParameter();
                        myparam.Direction = ParameterDirection.Input;
                        myparam.ParameterName = "@unitupc";
                        myparam.SqlDbType = SqlDbType.NVarChar;
                        myparam.Size = 50;
                        myparam.Value = comboBox3.Text;
                        command.Parameters.Add(myparam);
                        MSSQL.SqlDataAdapter myadapter = new System.Data.SqlClient.SqlDataAdapter();
                        myadapter.SelectCommand = command;
                        prodline.DataTable2.Clear(); //or u can use prodline.DataTable2.Reset() --Reset removes all data, indexes, relations, and columns of the table
                        myadapter.Fill(prodline, "DataTable2");
                        comboBox2.DataSource = prodline.DataTable2;
                        comboBox2.DisplayMember = "prodline";


                    }
                }
            }
            catch (Exception) { /*Handle error*/ }
        }
    }

你一直在填充数据表,所以先清除它:

prodLine.DataTable2.Rows.Clear();
myadapter.Fill(prodline, "DataTable2");