Get a single row of the datagridview

1

What I do is get a row from another datagridview1 just send it to call the list of the other datagridview2 and place it in the datagridview1. What I want to do is get only one row from the list, not all the data. I'm working with 3 layers. this would be my code of the form where I want to send the row datagridview2

O I WANTED TO KNOW IF THERE IS ANOTHER WAY TO SEND CALLING ROWS

private void FrmBuscarProducto_Load(object sender, EventArgs e)
{
    try
    {

        Nproducto gestionproductos = new Nproducto();
        dataGridView1.DataSource = gestionproductos.obtenerlistproducto();

    }
    catch (Exception ex)
    {

        MessageBox.Show(ex.Message, "ERROR");
    }
}

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    try
    {    

        idproducto = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["id_producto"].Value.ToString());
        costo = Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells["precio_ventas"].Value.ToString());
        tipo = dataGridView1.Rows[e.RowIndex].Cells["tipo"].Value.ToString();
        nombre = dataGridView1.Rows[e.RowIndex].Cells["nombre"].Value.ToString();
        cantidad = dataGridView1.Rows[e.RowIndex].Cells["cantidad"].Value.ToString();
        descripcion = dataGridView1.Rows[e.RowIndex].Cells["descripcion"].Value.ToString();
        DialogResult = DialogResult.OK;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

AND THIS WOULD BE THE CODE OF MY BUTTON WHERE I WILL ADD THE ROW

private void btnagregar_Click(object sender, EventArgs e)
{


    FrmBuscarProducto bp = new FrmBuscarProducto();
        if (bp.ShowDialog() == DialogResult.OK)
        {
        Nproducto gestionproductos = new Nproducto();
        dgvventas.DataSource = gestionproductos.obtenerlistproducto();         
    }


}
    
asked by gelder gomez 23.11.2017 в 05:01
source

1 answer

1

Good Gelder,

To do what you ask you can use SelectedRows of the DataGridView .

SelectedRows gets you the list of the selected rows if you have the Multiline property activated, but will give you the selected row (getting it as SelectedRows[0] ).

You should have something like this:

private void btnagregar_Click(object sender, EventArgs e) {
    FrmBuscarProducto bp = new FrmBuscarProducto();
    if (bp.ShowDialog() == DialogResult.OK)
    {
        Nproducto gestionproductos = new Nproducto();
        dgvventas.Rows.Add(dataGridView1.SelectedRows[0]);
    }
}

EDIT:

If you have the DataGridView linked to a DataTable , you must add the Row in DataTable and not in DataGridView .

    
answered by 23.11.2017 / 08:17
source