Send data from a Gridview in a Form to Combobox in another Form

Asked

Viewed 406 times

1

I’m trying to send the data of a gridviewin a second Formfor a comboboxin the form but I haven’t been successful. I wonder if there is a way to do this without sending as a parameter, since I will call the formmain at other times without needing this value. Follow the attempt 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 appear immediately on combobox "Computer" when user gives two clicks.

  • In the case of parameters you can leave it as Optional, already assigning a value to it type in the constructor for example public Form2(int? cbComputadorSelected = -1){ } When assigning to the combo you validate if there is value in the variable type cbComadorputSelected > -1 .. You would assign the index.. So you can call the main form at other times without having

  • Please exemplify with code snippet and add as answer to the question?

  • I’ll set an example to post

  • They’ve done it kkk

  • Thank you for your attention anyway...

  • @Managed to solve the problem? The answer meets the request or needs to improve on something?

  • It was very helpful. I’ve already marked it as a response. Thank you

Show 2 more comments

1 answer

2


Do it in a way that this parameter is not mandatory for the functioning of the form or create a public property in the second form that will receive this value.

Example:

  1. Optional parameter

    // construtor
    public Form1(int? valorOpcional)
    {
        if(valorOpcional == null)
            // Nenhum valor foi passado
        else
            cbComputador.SelectedIndex = (int)valorOpcional;
    }
    
  2. With the 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();
    }
    
  • I can receive the value correctly, but when using "cbComputer.Selectedindex = (int)pc;" (being pc my optional variable) it displays the error "Value of 'X' is not Valid for 'Selectedindex'." (where X is the value selected).

  • Well, this is because the value used is not a valid index. There is no way to respond better without more details. The implementation of the answer is right, it is a problem to capture the value (maybe a confusion between index and value?). You can open a new question with this problem too...

  • Another important detail that I didn’t add to the question is that initially the value of Combobox is filled with data from a table in my BD

  • If I use Selectedvalue instead of Selectedindex nothing happens. It displays nothing but the initial values of the BD

  • As I said, no more details is difficult. Opens a new question with all the necessary information.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.