Pass values from a DataGridView to ComboBox and TextBox

0

I have the following conflict, when trying to pass the values of each row of my datagrid to the other contours of my form since it only loads the values of the first row and I require that according to the row on which the user clicks. load the information of the first, second, third row etc, I leave the code that I am using to pass these values, I hope you can help me.

public void ValidaArticulosBD()
{
    try
    {
        if (txtFolio.Text != "")
        {
            folioText = txtFolio.Text;
        }
        else
        {
            folioText = "";
        }
        if (folioText!="")
        {
            dtBuscaDetalle = objConsultas.MuestraDetalleArticulos(folioText);
            if (dtBuscaDetalle.Rows.Count > 0)
            {
                dgvArticulos.DataSource = dtBuscaDetalle;
                ArticuloDGV = dgvArticulos.CurrentRow.Cells[1].Value.ToString();
                CantidadDGV = int.Parse(dgvArticulos.CurrentRow.Cells[2].Value.ToString());
                preciounitarioDGV = dgvArticulos.CurrentRow.Cells[3].Value.ToString();
                if (preciounitarioDGV.StartsWith("$"))
                {
                    preciounitarioDGV1 = preciounitarioDGV.TrimStart('$');
                    preciounitarioDGV2 = double.Parse(preciounitarioDGV1);
                }
                detalleDGV = dgvArticulos.CurrentRow.Cells[4].Value.ToString();
                DetalleDB = true;
            }
            else
            {
                dgvArticulos.DataSource = "";
                DetalleDB = false;
            }
        }
    }
    catch (Exception ex)
    {
        ex.ToString();
    }
}

private void dgvArticulos_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    try
    {
        ValidaArticulosBD();
        if (ArticuloDGV!="")
        {
            dtArticulos1 = objConsultas.BuscaArticulos(ArticuloDGV);
            if (dtArticulos1.Rows.Count > 0)
            {
                cmbIngresArticulo.Enabled = false;
                cmbIngresArticulo.DataSource = dtArticulos1;
                cmbIngresArticulo.ValueMember = "Art_ID";
                cmbIngresArticulo.DisplayMember = "Art_Descripcion";
            }
        }
        if (CantidadDGV > 0)
        {
            txtIngCantidad.Text = CantidadDGV.ToString();
        }
        if (preciounitarioDGV !="")
        {
            txtIngPU.Text = preciounitarioDGV2.ToString();
        }
        if (detalleDGV!="")
        {
            txtDescripcion.Text = detalleDGV;
        }
    }
    catch (Exception ex)
    {
        ex.ToString();
    }
}
    
asked by Alberto Arenas 26.01.2018 в 01:37
source

2 answers

1

I know a way, it is useful and quite simple, first of all, it involves two events:
- dataGridView1_CellClick
- dataGridView1_CellDoubleClick
The first method makes that when you select a row, whatever it is (by clicking on it) the index of the row (RowIndex) is stored, it would be like this:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{

    renglon = e.RowIndex; //La variable renglon debe estar previamente declarada y ser de tipo entero
}

Once you have the index of the row that is selected, you only extract the data from the dataGridView:

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    string nombre;
    if (e.RowIndex > -1 && e.ColumnIndex > -1) //Esta validacion es para que no te salte error si das clic en los encabezados o en la fila de la izquierda
    {
          nombre = dataGridView1.Rows[renglon_selec].Cells["NOMBRE"].Value.ToString(); //"NOMBRE" es el nombre de la columna en mi tabla, o puedes usar el indice de la columna [indice_columna]
          txtNombre.Text = nombre; //Aqui le asignas al TextBox lo que sacaste del dgv
    }
}

For the other fields you just have to do the same.

If it worked for you, do not forget to mark it: D

    
answered by 27.07.2018 в 00:58
0

As I understand you should go to the variable ArticleDGV, etc the data of the row you have selected:

ArticuloDGV = dgvArticulos.SelectedRows[0].Cells[1].Value.ToString();

and so on with the other variables. I hope I could help you.

    
answered by 26.01.2018 в 08:32