Good morning everyone, I'm using the following method to load a DataTable
that in turn will show this information within a DataGridView
, the problem I have is that by pressing the button I should add each new record to the DataTable
and show it in DataGrid
just update me the information of the first record and do not add the following line, who knows what I'm doing wrong? I attach the code.
public void CargarGrid()
{
try
{
ObtenerValores();
DataTable dtCGArt = new DataTable();
DataTable dtCGCte = new DataTable();
DataTable dtGridArticulos = new DataTable();
#region ObtenerArticulo
if (artId>0)
{
dtCGArt = objConsultas.MuestraArticulosID(artId);
if (dtCGArt.Rows.Count>0)
{
foreach (DataRow drCGArt in dtCGArt.Rows)
{
articulo = Convert.ToString(drCGArt[0]);
}
}
}
#endregion
#region ObtenerCliente
if (cteId>0)
{
dtCGCte = objConsultas.MuestraClientesID(cteId);
if (dtCGCte.Rows.Count>0)
{
foreach (DataRow drCGCte in dtCGCte.Rows)
{
cliente = Convert.ToString(drCGCte[0]);
}
}
}
#endregion
dtGridArticulos.Columns.Add("Articulo");
dtGridArticulos.Columns.Add("Cantidad");
dtGridArticulos.Columns.Add("PrecioUnitario");
dtGridArticulos.Columns.Add("Detalle");
DataRow drArticulosSeleccionados = dtGridArticulos.NewRow();
if (articulo!=""&&cliente!=""&&cantidad!=0&&preciounitario!=0&&descripcion!="")
{
drArticulosSeleccionados["Articulo"] = articulo;
drArticulosSeleccionados["Cantidad"] = cantidad;
drArticulosSeleccionados["PrecioUnitario"] = Convert.ToDouble(preciounitario);
drArticulosSeleccionados["Detalle"] = Convert.ToString(descripcion);
dtGridArticulos.Rows.Add(drArticulosSeleccionados);
if (dtGridArticulos.Rows.Count > 0)
{
foreach (DataRow drGridArticulos in dtGridArticulos.Rows)
{
cantidad1 = Convert.ToInt32(drGridArticulos[1]);
preciounitario1 = Convert.ToDouble(drGridArticulos[2]);
if (cantidad1>0&&preciounitario1>0)
{
subtotal = cantidad1 * preciounitario1;
txtIngSubtot.Text = subtotal.ToString();
if (subtotal > 0)
{
subtotal1 = subtotal * IVA;
totalfinal = subtotal + subtotal1;
if (totalfinal > 0)
{
txtIngTotal.Text = totalfinal.ToString();
}
else
{
txtIngTotal.Text = "0.0";
}
}
else
{
txtIngTotal.Text = "0.0";
}
}
}
dgvListArtIng.DataSource = dtGridArticulos;
dgvListArtIng.AllowUserToAddRows = false;
objValidaciones.MostrarAviso("Se agregaron los artículos de forma correcta.", false, lblAviso1);
}
else
{
dgvListArtIng.DataSource = "";
objValidaciones.MostrarAviso("No se encontraron artículos en esta orden", true, lblAviso1);
}
}
}
catch (Exception ex)
{
ex.ToString();
}
}
private void btnAgregaArt_Click(object sender, EventArgs e)
{
CargarGrid();
}