Event in ComboBox inside a Datagridview

4

I have a form where the header and detail of a Purchase Order exists. In the Load, load the detail data in a DataGridView with the CargarDetalle() method correctly.

My problem is that when I select an item of ComboBox , this event is not immediately recognized in the method CellValueChanged of DataGridView , as if the combo did not belong to it and does not update the other fields ( puniot and ptotal ) BUT when I then click on any other cell, YES it does ...

How can I generate the% com_com% share event mentioned?

    private void CargarDetalle()
    {
        dgvDetOC.Rows.Clear();
        if (TxtId.Text.Length > 0)  // es invisible. Solo se visualiza el Codigo correaltivo
        {
            oListaOrdencompras = oOrdencompra.GetAll();
            foreach (ordencompra orden in oListaOrdencompras)
            {
                if (orden.id == Convert.ToInt32(TxtId.Text))
                {
                    int i = 0;
                    foreach (det_ordencompra item in orden.det_ordencompras)
                    {
                        dgvDetOC.Rows.Add();
                        dgvDetOC.Rows[i].Cells[0].Value = item.id.ToString();   // col invisible
                        // En tiempo de Diseño se hacen estas dos cossas:
                        // Nombre de la celda donde está el combo: Unidad
                        // DataPropertyName: IdUnidad
                        DataGridViewComboBoxColumn CmbDetalle = dgvDetOC.Columns["Articulo"] as DataGridViewComboBoxColumn;
                        CmbDetalle.DataSource = oOrdenarArticulos;
                        CmbDetalle.DisplayMember = "nombre";
                        CmbDetalle.ValueMember = "id";
                        dgvDetOC.Rows[i].Cells[1].Value = item.idarticulo;
                        //
                        dgvDetOC.Rows[i].Cells[2].Value = item.punit;
                        dgvDetOC.Rows[i].Cells[3].Value = item.cantidad;
                        dgvDetOC.Rows[i].Cells[4].Value = item.ptotal;
                        dgvDetOC.Rows[i].Cells[5].Value = item.idordencompra.ToString();    // col invisible
                        i = i + 1;
                    }
                    break;
                }
            }
        }
    }



private void dgvDetOC_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvDetOC.RowCount > 0)
            {
                try
                {
                    // Al cambiar el Articulo en el combo
                    if (dgvDetOC.Columns[e.ColumnIndex].Name == "Articulo")
                    {
                        int IdArtic = Convert.ToInt32(dgvDetOC.Rows[e.RowIndex].Cells[1].Value);
                        articulo eArticulo = new articulo();
                        eArticulo = oArticulo.GetById(IdArtic);
                        if (eArticulo != null)
                        {
                            // Calculamos el nuevo punit y ptotal ---- ptotal = punit * cantidad
                            // Aqui la cantidad no varía
                            dgvDetOC.Rows[e.RowIndex].Cells[2].Value = eArticulo.pbruto;    // punit
                            dgvDetOC.Rows[e.RowIndex].Cells[4].Value = Convert.ToDecimal(dgvDetOC.Rows[e.RowIndex].Cells[2].Value) * Convert.ToDecimal(dgvDetOC.Rows[e.RowIndex].Cells[3].Value);   // ptotal
                            //dgvDetOC.CurrentCell = dgvDetOC.Rows[e.RowIndex].Cells[3];
                            //DtpFechaEmision.Focus();
                        }
                    }
                    // P.Unit o Cantidad
                    if (dgvDetOC.Columns[e.ColumnIndex].Name == "punit" || dgvDetOC.Columns[e.ColumnIndex].Name == "cantidad")
                    {
                        // ptotal = punit * cantidad
                        dgvDetOC.Rows[e.RowIndex].Cells[4].Value = Convert.ToDecimal(dgvDetOC.Rows[e.RowIndex].Cells[2].Value) * Convert.ToDecimal(dgvDetOC.Rows[e.RowIndex].Cells[3].Value);
                    }
                    // Totalizamos los montos de la OC
                    decimal TotalOrden = 0;
                    for (int i = 0; i <= dgvDetOC.RowCount - 1; i++)
                    {
                        TotalOrden = TotalOrden + Convert.ToDecimal(dgvDetOC.Rows[i].Cells[4].Value);  // ptotal
                    }
                    TxtVventa.Text = string.Format("{0:N2}", TotalOrden / (1 + Convert.ToDecimal(TxtPorcIgv.Text)));
                    TxtMtoIgv.Text = string.Format("{0:N2}", TotalOrden - Convert.ToDecimal(TxtVventa.Text));
                    TxtMonto.Text = string.Format("{0:N2}", TotalOrden);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Ingreso de Ítems", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    
asked by Mario Escudero 05.08.2016 в 15:07
source

1 answer

2

The issue is that the cell is put into edit and until you remove it does not terminate the operation on the cell so it does not throw the CellValueChanged

here

[DataGridView] - ComboBox and SelectedIndexChanged event

I explain this and how to use the EditingControlShowing to associate the SelectedIndexChanged of the ComboBox control contained within the cell

You will see how to use

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    DataGridViewComboBoxEditingControl dgvCombo = e.Control as DataGridViewComboBoxEditingControl;  

    if (dgvCombo != null)            
    {   
        //
        // se remueve el handler previo que pudiera tener asociado, a causa ediciones previas de la celda
        // evitando asi que se ejecuten varias veces el evento
        //
        dgvCombo.SelectedIndexChanged -= new  EventHandler(dvgCombo_SelectedIndexChanged);                

        dgvCombo.SelectedIndexChanged += new EventHandler(dvgCombo_SelectedIndexChanged);            
    }

}

private void dvgCombo_SelectedIndexChanged(object sender, EventArgs e) 
{
    //codigo
}

of this fotma you will have the event when you are changing the option of the combo

The assignment of events is done through the + = as explained here

How to: Subscribe and unsubscribe to events (C # Programming Guide)

    
answered by 05.08.2016 / 18:53
source