private void Button2_Click(object sender, EventArgs e)
        {
            if (dtgrCatalogo.SelectedRows.Count > 0)
            {
                int        codigo  = Convert.ToInt32(dtgrCompra.CurrentRow.Cells["codigoproducto"].Value);
                DtoDetalle detalle = new DtoDetalle();
                foreach (var item in detalles)
                {
                    if (item.CodigoProducto == codigo)
                    {
                        detalle = item;
                    }
                }

                detalles.Remove(detalle);
                dtgrCompra.DataSource = null;
                dtgrCompra.DataSource = detalles;
                dtgrCompra.Refresh();

                foreach (var item in listaAuxiliar)
                {
                    if (item.Codigo == codigo)
                    {
                        item.CantidadBodega = item.CantidadBodega + detalle.CantidadVendida;
                    }
                }
                dtgrCatalogo.DataSource = null;
                dtgrCatalogo.DataSource = listaAuxiliar;
                dtgrCatalogo.Refresh();
                txtTotal.Text = Convert.ToString(detalles.Sum(p => p.SubTotal));
            }
            else
            {
                MessageBox.Show("Seleccione una fila de la tabla");
            }
        }
        private void Añadir()
        {
            if (txtCantidad.Text == "" || Convert.ToInt32(txtCantidad.Text) == 0)
            {
                MessageBox.Show("Asegúrese de ingresar una cantidad a vender válida. No puede ser cero");
            }
            else
            {
                try
                {
                    if (dtgrCatalogo.SelectedRows.Count > 0)
                    {
                        int cantidad = Convert.ToInt32(txtCantidad.Text);
                        int stock    = Convert.ToInt32(dtgrCatalogo.CurrentRow.Cells["cantidadbodega"].Value);
                        if (cantidad > stock)
                        {
                            MessageBox.Show("La cantidad solicitada es mayor que la cantidad disponible");
                        }
                        else
                        {
                            int  codProducto = Convert.ToInt32(dtgrCatalogo.CurrentRow.Cells["codigo"].Value);
                            char encontro    = 'n';
                            foreach (var item in detalles)
                            {
                                if (item.CodigoProducto == codProducto)
                                {
                                    item.CantidadVendida  = item.CantidadVendida + Convert.ToInt32(txtCantidad.Text);
                                    item.SubTotal         = item.CantidadVendida * item.Costo;
                                    dtgrCompra.DataSource = null;
                                    dtgrCompra.DataSource = detalles;
                                    dtgrCompra.Refresh();


                                    encontro = 's';
                                }
                            }
                            if (encontro == 'n')
                            {
                                DtoDetalle detalle = new DtoDetalle();
                                detalle.CodigoProducto  = codProducto;;
                                detalle.NombreProducto  = Convert.ToString(dtgrCatalogo.CurrentRow.Cells["nombre"].Value);
                                detalle.Categoria       = Convert.ToString(dtgrCatalogo.CurrentRow.Cells["categoria"].Value);
                                detalle.Presentacion    = Convert.ToString(dtgrCatalogo.CurrentRow.Cells["presentacion"].Value);
                                detalle.CantidadVendida = Convert.ToInt32(txtCantidad.Text);
                                detalle.Costo           = Convert.ToDecimal(dtgrCatalogo.CurrentRow.Cells["costo"].Value);
                                detalle.SubTotal        = detalle.CantidadVendida * detalle.Costo;

                                detalles.Add(detalle);
                                dtgrCompra.DataSource = null;
                                dtgrCompra.DataSource = detalles;
                                dtgrCompra.Refresh();
                            }
                            foreach (var item in listaAuxiliar)
                            {
                                if (item.Codigo == Convert.ToInt32(dtgrCatalogo.CurrentRow.Cells["codigo"].Value))
                                {
                                    item.CantidadBodega = item.CantidadBodega - Convert.ToInt32(txtCantidad.Text);
                                }
                            }
                            dtgrCatalogo.DataSource = null;
                            dtgrCatalogo.DataSource = listaAuxiliar;
                            dtgrCatalogo.Refresh();
                            txtTotal.Text = Convert.ToString(detalles.Sum(p => p.SubTotal));
                        }
                    }
                    else
                    {
                        MessageBox.Show("Seleccione una fila de la tabla");
                    }
                }
                catch (Exception) { }
            }
        }