none
Why ComboBox.SelectedValue does not work before ParentChanged event? RRS feed

  • Question

  • Greetings to anyone reading this.
    I have a problem with run-time creation of a ComboBox in C# 2005.
    I created a DataTable, I filled it with data, and assigned it to the DataSource of the ComboBox.
    The problem is that the SelectedValue does not work; it
    has a null value, even after assign a value.
    The SelectedValue works only after I add the ComboBox to a parent control.
    What is the actual event that will cause the SelectedValue to work in cases that the DataSource, ValueMember and DisplayMember are used?
    I want to create a ComboBox that will have a selected value, other than the first one in the list, before adding it to a parent control. Is this possible, or do I have to use the workaround of adding it first to a parent control?

    Below you can find my source code in the Load event of my form that is named Form1.

          private void Form1_Load(object sender, EventArgs e)
          {
             DataTable table = new DataTable();
             DataRow row;
             table.Columns.Add("TEXT", typeof(String));
             table.Columns.Add("VALUE", typeof(int));

             row = table.NewRow();
             row["TEXT"] = "Text for row 1";
             row["VALUE"] = 1;
             table.Rows.Add(row);

             row = table.NewRow();
             row["TEXT"] = "Text for row 2";
             row["VALUE"] = 2;
             table.Rows.Add(row);

             row = table.NewRow();
             row["TEXT"] = "Text for row 3";
             row["VALUE"] = 3;
             table.Rows.Add(row);

             myComboBox.DataSource = table;
             myComboBox.DisplayMember = "TEXT";
             myComboBox.ValueMember = "VALUE";
             myComboBox.Top = 10;
             myComboBox.Left = 10;
             myComboBox.Height = 20;
             myComboBox.Width = 150;
             myComboBox.Enabled = true;
             myComboBox.Visible = true;

             myComboBox.SelectedValue = 2; //this will not work!!!! make a breakpoint and watch the value
             myComboBox.Parent = this;
             myComboBox.SelectedValue = 2; //this works!!!

             /* Replace the previous 3 lines of code with
              * the following...
             myComboBox.SelectedValue = 2; //this will not work!!!!
             this.Controls.Add(myComboBox);
             myComboBox.SelectedValue = 2; //this works!!!
             */

          }

    Thank you in advance
    Monday, July 9, 2007 11:31 AM

Answers

  •  

    Hi DIoannidis,

     

    It is because that there is no BindingManagerBase. By default, controls use form's BindingManagerBase to store the current position if a control has been add to the from. If the control is not added to the form, these is no BindingManagerBase. So you need to define your own BindingManagerBase.

    Code Snippet

                DataTable table = new DataTable();

                DataRow row;

                table.Columns.Add("TEXT", typeof(String));

                table.Columns.Add("VALUE", typeof(int));

     

                row = table.NewRow();

                row["TEXT"] = "Text for row 1";

                row["VALUE"] = 1;

                table.Rows.Add(row);

     

                row = table.NewRow();

                row["TEXT"] = "Text for row 2";

                row["VALUE"] = 2;

                table.Rows.Add(row);

     

                row = table.NewRow();

                row["TEXT"] = "Text for row 3";

                row["VALUE"] = 3;

                table.Rows.Add(row);

     

                myComboBox.BindingContext = new BindingContext();

                myComboBox.DataSource = table;

                myComboBox.DisplayMember = "TEXT";

                myComboBox.ValueMember = "VALUE";

                myComboBox.Top = 25;

                myComboBox.Left = 10;

                myComboBox.Height = 20;

                myComboBox.Width = 150;

                myComboBox.Enabled = true;

                myComboBox.Visible = true;

                myComboBox.BindingContext = new BindingContext();

     

                myComboBox.SelectedValue = 2; //this will work!!!! make a breakpoint and watch the value

     

    Hope this helps.

    Regards

    Tuesday, July 10, 2007 11:47 AM

All replies

  •  

    Hi DIoannidis,

     

    It is because that there is no BindingManagerBase. By default, controls use form's BindingManagerBase to store the current position if a control has been add to the from. If the control is not added to the form, these is no BindingManagerBase. So you need to define your own BindingManagerBase.

    Code Snippet

                DataTable table = new DataTable();

                DataRow row;

                table.Columns.Add("TEXT", typeof(String));

                table.Columns.Add("VALUE", typeof(int));

     

                row = table.NewRow();

                row["TEXT"] = "Text for row 1";

                row["VALUE"] = 1;

                table.Rows.Add(row);

     

                row = table.NewRow();

                row["TEXT"] = "Text for row 2";

                row["VALUE"] = 2;

                table.Rows.Add(row);

     

                row = table.NewRow();

                row["TEXT"] = "Text for row 3";

                row["VALUE"] = 3;

                table.Rows.Add(row);

     

                myComboBox.BindingContext = new BindingContext();

                myComboBox.DataSource = table;

                myComboBox.DisplayMember = "TEXT";

                myComboBox.ValueMember = "VALUE";

                myComboBox.Top = 25;

                myComboBox.Left = 10;

                myComboBox.Height = 20;

                myComboBox.Width = 150;

                myComboBox.Enabled = true;

                myComboBox.Visible = true;

                myComboBox.BindingContext = new BindingContext();

     

                myComboBox.SelectedValue = 2; //this will work!!!! make a breakpoint and watch the value

     

    Hope this helps.

    Regards

    Tuesday, July 10, 2007 11:47 AM
  • Thank you for your reply.

    The solution you provided works.

    Thursday, July 12, 2007 7:03 AM