hoek rand
hoek rand

Reputation: 329

Having trouble trying to generate a lot of comboboxes which have the contents of a mysql database as source

I'm trying to make a WPF application. I have a button that generates 100 comboboxes in a grid. I want these comboboxes to inherit the values inside a mysql table. So i want all of these 100 comboboxes to have the same values inside the comboboxes. So for instance, I have a table named test that has and ID(auto incremented) and a number. Inside this table I've added 3 entries, 1,2 and 3. I want all my comboboxes to show 1,2 and 3 under it.

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


        }

This is the function I use. When i click my button, this function gets called. What I see is a grid of 7 by 100: enter image description here

The following code:

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

Is working and retrieves data and appends it to the combobox. How can I make it so every first combobox per row appends data retrieved from a mysql table? Sorry for my English, it's not my native language.

edit1: I used the FillComboBox method Bradley Granger specified, and added DropDownOpened and SelectionChanged functions to solve my problems. It works perfectly, and runs without problems. My computer can easily handle the amount of MySQL queries.

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

Upvotes: 0

Views: 40

Answers (2)

Inside a method create a List and add your values to the List that you need on combo boxes. Then create another method to assign that List to all of your combo boxes. Then call both of the functions in form load event.

I think this is the simplest way.

Upvotes: 0

Bradley Grainger
Bradley Grainger

Reputation: 28172

Your loop in Add_New_Entry creates 100 combo boxes. What you need to do is fill each combo box in each iteration of the loop.

You could change (or copy) your aanPinStekKast method to take a ComboBox as a parameter and add to its .Items collection.

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
}

Note that if you want each combo box to have the exact same collection of items, making one query per combo box is expensive (and may hang the UI). To fix this, create a collection (e.g., List<string>) of items from the database once, then use that in-memory collection to initialise each ComboBox.

Upvotes: 1

Related Questions