Send data from a GridView on a Form to ComboBox on another Form

1

I'm trying to send the data from a gridview in a second Form to a combobox in form master but I have not been successful. I would like to know if there is a way to do this without being sent as a parameter, since I will call the form master at other times without needing this value. Here is the try code:

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    Form1 f = new Form1();
    f.cbComputador.SelectedIndex = Convert.ToInt32(dataGridView1.CurrentRow.Cells[1].Value);
    this.Close();
}

I need the value to immediately appear on the% computer "%" when the user clicks twice.

    
asked by anonymous 07.06.2017 / 00:40

1 answer

2

Make a way in which this parameter is not mandatory for form to work, or create a public property in the second form that will receive this value. >

Example:

  • Optional parameter

    // construtor
    public Form1(int? valorOpcional)
    {
        if(valorOpcional == null)
            // Nenhum valor foi passado
        else
            cbComputador.SelectedIndex = (int)valorOpcional;
    }
    
  • With property

    public class Form1
    {
        public int? ValorDoCombo { get; set; }
    
        public Form1() 
        { 
            if(ValorDoCombo != null)
                cbComputador.SelectedIndex = (int)ValorDoCombo;
    
        }
    }
    

    And the use would be something like

    private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        Form1 form = new Form1();
        form.ValorDoCombo = Convert.ToInt32(dataGridView1.CurrentRow.Cells[1].Value);
        this.Close();
    }
    
  • 07.06.2017 / 01:56