Resolve error Value of 'X' is not valid for 'SelectedIndex', where X is any Index value

0

Hello. I asked a question days ago about capturing the value of a cell from a gridview to a Form leg% and sending it to a combobox in the Form primary. I received a satisfactory response and that causes the Form primary to receive the value, however when I try to change the index from combobox to the index I received the error message Value of 'X' is not valid for ' SelectedIndex ', where X is the received value. It is worth mentioning that when starting the application this same combobox cbComputer is filled with data from a DB table, according to code. I would like to know if there is any way to resolve this impasse.

% Fill_code%:

public DataTable GetIdentificador()
    {
        DataTable dataUf = new DataTable();
        bdConn = new MySqlConnection("Persist Security Info=False;server=localhost;database=controle;uid=root;pwd=''");
        try
        {
            bdConn.Open();
            if (bdConn.State == ConnectionState.Open)
            {
                //Se estiver aberta faz a consulta dos dados do BD
                MySqlCommand cmd = new MySqlCommand("SELECT identificador FROM computador WHERE status=0", bdConn);
                dataUf.Load(cmd.ExecuteReader());
                cmd.Dispose();
            }
            bdConn.Close();
            bdConn.Dispose();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Impossível estabelecer conexão.\n" + ex.Message);
        }

        return dataUf;

    }

When I load combobox with the above method values:

public void Form1_Load(object sender, EventArgs e)
    {
        cbComputador.ValueMember = "identificador";
        cbComputador.DisplayMember = "identificador";
        cbComputador.DataSource = GetUF();
    }

Link to the question I asked earlier with the answer here . In this case, I am using the first option given in the answer. If it was not clear enough, please comment that I try to explain it again in more detail.
Thank you for your attention.

    
asked by anonymous 08.06.2017 / 02:22

2 answers

2

It appears that the value returned from the GridView should not be an existing index in the combobox.

Is the return value of this column really an index or would it be an Identifier for each UF?

  dataGridView1.CurrentRow.Cells[1].Value

If it is an Identifier, as I imagine it should be used

08.06.2017 / 14:07
1

I was able to resolve the problem by using the response posted here along with the response from this link . I hope it helps someone else.

    
09.06.2017 / 00:09