Search code examples
c#datagridviewcomboboxmdi

Combobox value changes when opening same MDI child


I open an MDI child when I double click on a DataGridViewRow. In the MDI child the values for this chosen row are shown. One of the values is displayed in a combobox. When I open the first MDI child all goes well and the combobox shows the correct value (the correct console).

However, when I open a similar second MDI child choosing another row from the DataGridView, the comboboxvalue in the first MDI child changes to the value that has to be shown in the second MDI child. All other textboxvalues in the first MDI child form are still displayed correctly.

Does anyone have a solution for this problem?

MDI Parent form

private void dataGridViewGames_DoubleClick(object sender, EventArgs e)
    {
        FormGame formGame = new FormGame();
        formGame.MdiParent = this.MdiParent;
        formGame.Name = dataGridViewGames.SelectedRows[0].Index.ToString();
        formGame.Rij = dataGridViewGames.SelectedRows[0].Index;
        formGame.Consoles = consoles;
        formGame.Games = games;
        formGame.Show();
        formGame.LeesGame();
    }

MDI Child form

private void FormGame_Load(object sender, EventArgs e)
    {
        comboBoxConsole.DataSource = consoles;
        comboBoxConsole.DisplayMember = "Naam";
        comboBoxConsole.ValueMember = "Id";
    }

    public void LeesGame()
    {
        DBGames.GameRow gameRij = (DBGames.GameRow)games.Rows[rij];
        this.Text = "Game - " + gameRij.Naam;
        textBoxNaam.Text = gameRij.Naam;
        textBoxPrijs.Text = gameRij.Prijs.ToString();
        textBoxAfbeelding.Text = gameRij.Afbeelding;
        comboBoxConsole.SelectedValue = gameRij.ConsoleId;
    }

Solution

  • I suspect it's because you are setting the combobox source to "consoles" and that when the second child is opened, the context of "consoles" has changed (pointing to a different record. You'd need two separate instances of the "consoles" data source to feed two distinct combo boxes.

    In other words, if there is just one "consoles" object, and it's owned by the MDI parent, then both MDI children rely upon it. Change that, and you change all MDI children's display of that data.