C# er(人口2); Grid.SetRow(组合框,num); Grid.SetColumn(组合框,6); } 私有void FillSeparator1(分隔符分隔符,int num) { 分离器,高度=30; Grid.SetRow(分隔符,num); 网格设置列(分隔符,2); } 私有void FillSeparator2(分隔符分隔符,int num) { 分离器,高度=30; Grid.SetRow(分隔符,num); 网格设置列(分隔符,4); }

C# er(人口2); Grid.SetRow(组合框,num); Grid.SetColumn(组合框,6); } 私有void FillSeparator1(分隔符分隔符,int num) { 分离器,高度=30; Grid.SetRow(分隔符,num); 网格设置列(分隔符,2); } 私有void FillSeparator2(分隔符分隔符,int num) { 分离器,高度=30; Grid.SetRow(分隔符,num); 网格设置列(分隔符,4); },c#,mysql,wpf,combobox,C#,Mysql,Wpf,Combobox,您在添加新条目中的循环创建100个组合框。您需要做的是在循环的每次迭代中填充每个组合框 您可以更改(或复制)您的aanPinStekKast方法,将组合框作为参数并添加到其集合中 private void Add_New_Entry(object sender, RoutedEventArgs e) { grid.RowDefinitions.Clear(); for (int x = 0; x < number; x++) { grid.RowDe

您在
添加新条目中的循环创建100个组合框。您需要做的是在循环的每次迭代中填充每个组合框

您可以更改(或复制)您的
aanPinStekKast
方法,将
组合框
作为参数并添加到其
集合中

private void Add_New_Entry(object sender, RoutedEventArgs e)
{
    grid.RowDefinitions.Clear();
    for (int x = 0; x < number; x++)
    {
        grid.RowDefinitions.Add(new RowDefinition());

        // ...

        aansluitpin1 = new ComboBox();
        Grid.SetRow(aansluitpin1, x);
        Grid.SetColumn(aansluitpin1, 1);

        // THIS IS THE CHANGE
        // fill each combo box as you create it
        FillComboBox(aansluitpin1);

        // ...
    }
}

private void FillComboBox(ComboBox comboBox)
{
    // same code as aanPinStekKast
    // but modify 'comboBox' instead of AantalPinnenStekkersTestkast
}
private void Add\u New\u条目(对象发送方、路由目标方)
{
grid.RowDefinitions.Clear();
对于(int x=0;x

请注意,如果您希望每个组合框都有完全相同的项目集合,那么对每个组合框进行一次查询是非常昂贵的(并且可能会挂起UI)。要解决此问题,请从数据库中创建一次项集合(例如,
列表
),然后使用该内存集合初始化方法中的每个
组合框
,创建一个列表,并将您的值添加到组合框上所需的列表中。然后创建另一个方法,将该列表分配给所有组合框。 然后在表单加载事件中调用这两个函数

我认为这是最简单的方法

private void aanPinStekKast(object sender, EventArgs e)
        {
            //ClearTable();
            try
            {
                //multistore.Clear();
                AantalPinnenStekkersTestkast.Items.Clear();
                connection.Open();
                // Deze SQL string maakt een table aan met daarin de parameters.
                sqlstring = "SELECT * FROM test";
                MySqlCommand cmd = new MySqlCommand(sqlstring, connection);
                MySqlDataReader rdr;
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    AantalPinnenStekkersTestkast.Items.Add(rdr["number"]);
                }
                cmd.Dispose();

                connection.Close();
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message + connectionString);
                //button1.BackColor = Color.Red;
                connection.Close();
            }
        }
private void Add_New_Entry()
        {
            connection.Open();
            for (int x = 0; x < number; x++)
            {
                grid.RowDefinitions.Add(new RowDefinition());
                stekker1 = new ComboBox();
                aansluitpin1 = new ComboBox();
                sep = new Separator();
                lab1 = new Label();
                sep1 = new Separator();
                stekker2 = new ComboBox();
                aansluitpin2 = new ComboBox();


                FillStekker1(stekker1,x);
                FillAansluitpin1(aansluitpin1,x);
                FillSeparator1(sep, x);
                FillLabel(lab1, x);
                FillSeparator2(sep1,x);
                FillStekker2(stekker2,x);
                FillAansluitpin2(aansluitpin2,x);

                grid.Children.Add(stekker1);
                grid.Children.Add(aansluitpin1);
                grid.Children.Add(sep);
                grid.Children.Add(lab1);
                grid.Children.Add(sep1);
                grid.Children.Add(stekker2);
                grid.Children.Add(aansluitpin2);
            }
            connection.Close();
        }
        #endregion
        #region set parameters van elementen in grid
        private void FillLabel(Label lab, int num)
        {
            lab.Height = 30;
            int labelnumber = num + 1;
            lab.Content = labelnumber.ToString();
            lab.HorizontalAlignment = HorizontalAlignment.Center;
            Grid.SetRow(lab, num);
            Grid.SetColumn(lab, 3);
        }

        private void FillStekker1(ComboBox comboBox, int num)
        {
            comboBox.ItemsSource = numlistTestkast;
            comboBox.SelectionChanged += Aansluitpin1_SelectionChanged;
            comboBox.Height = 30;
            Grid.SetRow(comboBox, num);
            Grid.SetColumn(comboBox, 0);
        }
        private void populateElement1(object sender, EventArgs e)
        {

            try
            {
                ComboBox cb = sender as ComboBox;
                cb.Items.Clear();
                connection.Open();
                string num = aPin1;
                sqlstring = string.Format("SELECT * FROM testkastpins WHERE stekkernummer = {0}",num);
                MySqlCommand cmd = new MySqlCommand(sqlstring, connection);
                MySqlDataReader rdr;
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    cb.Items.Add(rdr["pinnummer"]);
                }
                cmd.Dispose();

                connection.Close();
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message + connectionString);
                connection.Close();
            }
        }
        private void populateElement2(object sender, EventArgs e)
        {

            try
            {
                ComboBox cb = sender as ComboBox;
                cb.Items.Clear();
                connection.Open();
                string num = aPin2;
                sqlstring = string.Format("SELECT * FROM testpaneelpins WHERE stekkernummer = {0}", num);
                MySqlCommand cmd = new MySqlCommand(sqlstring, connection);
                MySqlDataReader rdr;
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    cb.Items.Add(rdr["pinnummer"]);
                }
                cmd.Dispose();

                connection.Close();
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message + connectionString);
                connection.Close();
            }
        }
        private void FillStekker2(ComboBox comboBox, int num)
        {
            comboBox.Height = 30;
            comboBox.SelectionChanged += Aansluitpin2_SelectionChanged;
            comboBox.ItemsSource = numlistTestpaneel;
            Grid.SetRow(comboBox, num);
            Grid.SetColumn(comboBox, 5);
        }
        private void FillAansluitpin1(ComboBox comboBox, int num)
        {
            comboBox.Height = 30;
            //comboBox.ItemsSource = store1;
            comboBox.DropDownOpened += new EventHandler(populateElement1);
            Grid.SetRow(comboBox, num);
            Grid.SetColumn(comboBox, 1);
        }
        private void Aansluitpin1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            aPin1 = cb.SelectedValue.ToString();
            debug.Text += aPin1;
        }
        private void Aansluitpin2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            aPin2 = cb.SelectedValue.ToString();
            debug.Text += aPin2;
        }

        private void FillAansluitpin2(ComboBox comboBox, int num)
        {
            comboBox.Height = 30;
            comboBox.DropDownOpened += new EventHandler(populateElement2);
            Grid.SetRow(comboBox, num);
            Grid.SetColumn(comboBox, 6);
        }
        private void FillSeparator1(Separator separator, int num)
        {
            separator.Height = 30;
            Grid.SetRow(separator, num);
            Grid.SetColumn(separator, 2);
        }

        private void FillSeparator2(Separator separator, int num)
        {
            separator.Height = 30;
            Grid.SetRow(separator, num);
            Grid.SetColumn(separator, 4);
        }
private void Add_New_Entry(object sender, RoutedEventArgs e)
{
    grid.RowDefinitions.Clear();
    for (int x = 0; x < number; x++)
    {
        grid.RowDefinitions.Add(new RowDefinition());

        // ...

        aansluitpin1 = new ComboBox();
        Grid.SetRow(aansluitpin1, x);
        Grid.SetColumn(aansluitpin1, 1);

        // THIS IS THE CHANGE
        // fill each combo box as you create it
        FillComboBox(aansluitpin1);

        // ...
    }
}

private void FillComboBox(ComboBox comboBox)
{
    // same code as aanPinStekKast
    // but modify 'comboBox' instead of AantalPinnenStekkersTestkast
}