在尝试生成大量以mysql数据库的内容为源的组合框时遇到问题

uqdfh47h  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(419)

我正在申请wpf。我有一个按钮,在网格中生成100个组合框。我希望这些组合框继承mysql表中的值。所以我希望所有这100个组合框在组合框中有相同的值。例如,我有一个名为test的表,它有和id(自动递增)和一个数字。在这个表中,我添加了3个条目,1、2和3。我希望我所有的组合框下显示1,2和3。

private void Add_New_Entry(object sender, RoutedEventArgs e)
        {
            grid.RowDefinitions.Clear();
            for (int x = 0; x < number; x++)
            {
                grid.RowDefinitions.Add(new RowDefinition());
                stekker1 = new ComboBox();
                stekker1.Name = "testkastComboBox" + number.ToString();
                stekker1.ItemsSource = multistore;

                Grid.SetRow(stekker1, x);
                Grid.SetColumn(stekker1, 0);

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

                sep = new Separator();
                Grid.SetRow(sep, x);
                Grid.SetColumn(sep, 2);

                lab1 = new Label();
                int labelnumber = x + 1;
                lab1.Content = labelnumber.ToString();
                lab1.HorizontalAlignment = HorizontalAlignment.Center;
                Grid.SetRow(lab1, x);
                Grid.SetColumn(lab1, 3);

                sep1 = new Separator();
                Grid.SetRow(sep1, x);
                Grid.SetColumn(sep1, 4);

                stekker2 = new ComboBox();
                Grid.SetRow(stekker2, x);
                Grid.SetColumn(stekker2, 5);

                aansluitpin2 = new ComboBox();
                Grid.SetRow(aansluitpin2, x);
                Grid.SetColumn(aansluitpin2, 6);

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

        }

这就是我使用的函数。当我点击我的按钮时,这个函数被调用。我看到的是7乘100的网格:

以下代码:

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

正在工作,检索数据并将其附加到组合框。如何使每行的第一个组合框都附加从mysql表检索到的数据?对不起,我的英语不是我的母语。
edit1:我使用bradley granger指定的fillcombobox方法,并添加了dropdownopened和selectionchanged函数来解决我的问题。它工作完美,运行没有问题。我的电脑可以轻松处理大量的mysql查询。

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

flseospp1#

你的循环 Add_New_Entry 创建100个组合框。您需要做的是在循环的每次迭代中填充每个组合框。
你可以更改(或复制)你的 aanPinStekKast 采取措施 ComboBox 作为参数并添加到 .Items 收藏。

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
}

请注意,如果您希望每个组合框都有完全相同的项集合,那么对每个组合框进行一次查询是非常昂贵的(并且可能会挂起ui)。要解决此问题,请创建一个集合(例如。, List<string> )对数据库中的项进行一次初始化,然后使用内存中的集合对每个项进行初始化 ComboBox .

2hh7jdfx

2hh7jdfx2#

在方法内部创建一个列表,并将值添加到组合框上所需的列表中。然后创建另一个方法将该列表分配给所有组合框。然后在form load事件中调用这两个函数。
我认为这是最简单的方法。

相关问题