I'm working on a Windows Forms application created in Visual Studio 2010.
Working with 2 different forms, I have a ComboBox on the first form that is databound to the client table of my database. Also on the first form is a link-label that opens the second form without closing or hiding the first form. The second form is just a textbox used to insert a new row into my client table.

The second form successfully inserts the new row into the database and closes itself.

The question that I have is:

How do I get the comboBox on form1 to refresh it's contents after they have been updated by form2?

I did the example code, and it works fine. I have created two form (form1, and form2). Instead of using dropDownList, I have used ComboBox control - its actually the same, you only change the code so it will suit to your control. Ok here it is:


//form1:

public partial class Form1 : Form
 {
  Form2 form2;

  public Form1()
  {
   InitializeComponent();
   this.StartPosition = FormStartPosition.CenterScreen;
   OpeningForm2();

   this.comboBox1.DataSource = form2.list;
   this.comboBox1.DisplayMember = "name";
   this.comboBox1.ValueMember = this.comboBox1.DisplayMember;
   this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
  }

  private void OpeningForm2()
  {
   form2 = new Form2();
   //location Form2 beside Form1 (on right side):
   int x = Screen.PrimaryScreen.Bounds.Width / 2;
   int y = Screen.PrimaryScreen.Bounds.Height / 2;
   form2.Location = new Point(x, y);
   form2.Show();
  }
 }

//form2:
 public partial class Form2 : Form
 {
  public BindingList<Country> list;

  public Form2()
  {
   InitializeComponent();
   list = new BindingList<Country>();

   this.listBox1.DataSource = list;
   this.listBox1.DisplayMember = "name";
   this.listBox1.ValueMember = this.listBox1.DisplayMember;

   this.textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
  }

  private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  {
   if (e.KeyChar == (char)Keys.Enter)
   {
    string item = this.textBox1.Text.Trim();
    if (item != String.Empty)
    {
     Country c = new Country();
     c.name = item;
     list.Add(c);
     //clearing:
     this.textBox1.Text = "";
     
     
    }
   }
  }
 }

//and additionaly class of Country
 public class Country
 {
  public string name { get; set; }
 }

You can find the whole project HERE.

Code is working like you wanted - when the user inserts some text into textbBox on Form2, the string transfers to generic list <T> which is a binding source of listBox (on form2) and comboBox (dropDownList) on form1, the value appea in both this dataBinded controls.

Hope it helps,

Mitja

What I was missing was a way to reload the table adapter for my comboBox.
I managed to do it through an event handler.

private void comboBox1_Enter(object sender, EventArgs e)
        {
            this.clientTableAdapter.Fill(this._projectDataSet.client);
        }

This basically makes the comboBox reload the the table from the dataset each time that it is interacted with.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.