Resolve error Value of 'X' is not Valid for 'Selectedindex', where X is any Index value

Asked

Viewed 51 times

0

Hello.
I asked a question days ago about capturing the value of a cell gridview in a Form secondary and send it to a combobox in the Form primary. I have received a satisfactory response which causes the Form primary receives the value, however when I try to change the content of the combobox for the index I received the error message Value of 'X' is not Valid for 'Selectedindex', where X is the value received.
It is worth noting that when starting the application this same combobox cbComputer is filled with data from a BD table according to code. I wonder if there is any way to resolve this impasse.

Code for completing the combobox:

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 carry combobox with the values of the above method:

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 reply.
If not clear enough, please comment that I try to explain again in more detail.
Thank you for your attention.

2 answers

2


It seems that the value returned from Gridview should not be an existing index in the combobox.

The return value of this column is actually an index or would be an identifier of each UF?

  dataGridView1.CurrentRow.Cells[1].Value

If it is an Identifier, as I imagine it should be used the 'Selectedvalue' instead of 'Selectedindex'

  • When I use the Selectedvalue it does not change what is being displayed in the combobox

  • It seems that the value is lost once I insert a gridview in the same Form and run the same line with Selectedvalue it works normally.

1

I was able to solve the problem by using the answer published here along with the reply of this link. I hope you help someone else.

Browser other questions tagged

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