ComboBox control inside DataRepeater control does not correctly filled by data from database (MySQL server)

353 views Asked by At

I have one winform contains one Datarepeater control with one nested ComboBox control. In additon i have next one ComboBox control placed directly in WinForm body. I use below method to insert data from my database to comboboxes. Those method are called in Load event. Finally ComboBox nested in DR control does not contains any data but ComboBox in WinForm is correctly filled with data from database. Would you mind somebody telling me WHY?

  • for Combobox control nested in DataRepeater control:

    private void laduj_Masa_wych_cbx()
       {
           try
           {
              da1_przych = new MySqlDataAdapter(query_test, connection);
              DataTable ddtt = new DataTable();
              da1_przych.Fill(ddtt);
              foreach (DataRow row in ddtt.Rows)
              {
                  Masa_wych_cbx.Items.Add(row["masa"]);
              }
           }
           catch (Exception ee)
           {
              MessageBox.Show(ee.Message);
           }
       }
    
  • for ComboBox control placed directly in DataRepeater control:

    private void laduj_Masa1_wych_cbx()
       {
           try
           {
               da1_przych = new MySqlDataAdapter(query_test, connection);
               DataTable ddtt = new DataTable();
               da1_przych.Fill(ddtt);
               foreach (DataRow row in ddtt.Rows)
               {
                  Masa1_wych_cbx.Items.Add(row["masa"]);
               }
           }
           catch (Exception ee)
           {
                MessageBox.Show(ee.Message);
           }
       }
    

In result ComboBox nested in DR control does not contains any data but ComboBox managed by WinForm contains correctly list from a database. My second approch looked like below. I changed both above methods with two methods shown below. In this case both ComboBox controls are correctly filled with data.

  • for Combobox control nested in DataRepeater control:

    private void laduj_Masa_wych_cbx()
    {
        this.Masa_wych_cbx.Items.Add("0");
        this.Masa_wych_cbx.Items.Add("50");
        this.Masa_wych_cbx.Items.Add("100");
        this.Masa_wych_cbx.Items.Add("130");
        this.Masa_wych_cbx.Items.Add("350");
        this.Masa_wych_cbx.Items.Add("500");
        this.Masa_wych_cbx.Items.Add("1000");
        this.Masa_wych_cbx.Items.Add("1500");
        this.Masa_wych_cbx.Items.Add("2000");
    }
    
  • for ComboBox control placed directly in DataRepeater control:

    private void laduj_Masa1_wych_cbx()
    {
        this.Masa1_wych_cbx.Items.Add("0");
        this.Masa1_wych_cbx.Items.Add("50");
        this.Masa1_wych_cbx.Items.Add("100");
        this.Masa1_wych_cbx.Items.Add("130");
        this.Masa1_wych_cbx.Items.Add("350");
        this.Masa1_wych_cbx.Items.Add("500");
        this.Masa1_wych_cbx.Items.Add("1000");
        this.Masa1_wych_cbx.Items.Add("1500");
        this.Masa1_wych_cbx.Items.Add("2000");
    }
    

Unfortunately i need to fill data to ComboBoxes dynamicly but it does not work inside DataRepeater control - what to do to repair this state?

1

There are 1 answers

0
Mikhail Timofeev On

Try this:

        ListItem li = new ListItem(row["masa"]);
        Masa1_wych_cbx.Items.Add(li);