c# – Error when passing parameters from the grid view of a form in the combo box

Question:

Good morning, I have a form where I consult my budgets and in it I have a grid view .

When I double click on the grid line it opens another form for me. But the data that is in the line of my grid view appear to me the first item of my combo appears.

This is the event and the code I use to make it open on the other form. This form is the query:

private void dgvOrc_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    frmOrcDomestico formOrc = new frmOrcDomestico();
    formOrc.PreencheDados(dgvOrc.SelectedRows[0].Cells[0].Value.ToString(),
    dgvOrc.SelectedRows[0].Cells[1].Value.ToString(),
    dgvOrc.SelectedRows[0].Cells[2].Value.ToString(),
    dgvOrc.SelectedRows[0].Cells[3].Value.ToString(),
    dgvOrc.SelectedRows[0].Cells[4].Value.ToString(),
    dgvOrc.SelectedRows[0].Cells[5].Value.ToString(),
    dgvOrc.SelectedRows[0].Cells[6].Value.ToString(),
    dgvOrc.SelectedRows[0].Cells[7].Value.ToString(),
    dgvOrc.SelectedRows[0].Cells[8].Value.ToString());
    formOrc.ShowDialog();
} 

This form is what will receive the data from the other.

public void PreencheDados(string id, string c, string p, string t, string dom, string data, string l, string d, string pg)
{
    lblId.Text = id;

    comboCliente.Text = c;

    comboProduto.Text = p;

    comboTipoProduto.Text = t;

    lblDomestico.Text = dom;

    lbld.Text = data;

    lblStatus.Text = l;

    textDescricao.Text = d;

    maskValor.Text = pg;    
}

Answer:

Since it's in the CellContentDoubleClick event replace dgvOrc.SelectedRows[0] by dgvOrc.Rows[e.RowIndex] which is sure to request the value of the row clicked.

    private void dgvOrc_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
       frmOrcDomestico formOrc = new frmOrcDomestico();
       formOrc.PreencheDados(
         dgvOrc.Rows[e.RowIndex].Cells[0].Value.ToString(),
         dgvOrc.Rows[e.RowIndex].Cells[1].Value.ToString(),
         dgvOrc.Rows[e.RowIndex].Cells[2].Value.ToString(),
         dgvOrc.Rows[e.RowIndex].Cells[3].Value.ToString(),
         dgvOrc.Rows[e.RowIndex].Cells[4].Value.ToString(),
         dgvOrc.Rows[e.RowIndex].Cells[5].Value.ToString(),
         dgvOrc.Rows[e.RowIndex].Cells[6].Value.ToString(),
         dgvOrc.Rows[e.RowIndex].Cells[7].Value.ToString(),
         dgvOrc.Rows[e.RowIndex].Cells[8].Value.ToString());
   }

This line can be wrong depending on how the combobox is: comboProduto.Text = p;

Try one of these lines:

comboProduto.SelectedValue = p;

Where

comboProduto.SelectedText = p;
Scroll to Top