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);
}
}
}