Format currency to DataGridView content

0

Good morning to all, I am trying by means of the following codes to format the currency of the contents of several cells within my DataGridView, which I do not get, if you can help me I will be very grateful.

    dgvListArtIng.Columns["PrecioUnitario"].DefaultCellStyle.Format= "C2";
    dgvListArtIng.Columns["SubtotalArticulo"].DefaultCellStyle.Format ="C2";

    dgvListArtIng.Columns["PrecioUnitario"].DefaultCellStyle.Format= "N2";
    dgvListArtIng.Columns["SubtotalArticulo"].DefaultCellStyle.Format = "N2";

    dgvListArtIng.Columns["PrecioUnitario"].DefaultCellStyle.Format= "##,##0.00";
    dgvListArtIng.Columns["SubtotalArticulo"].DefaultCellStyle.Format = "##,##0.00";

When I apply one of these options, it keeps showing me the values of each cell as integers and I need to show them in the "$ 10,000.00" currency format.

I add the method that fills the DataGridView

 public void CargarGridArticulos()
    {
        #region Objetos
        DataTable dtCGArt = new DataTable();
        #endregion
        #region Variables
        int cantidad = 0;
        double preciounitario = 0;
        string descripcion = "";
        double subtotalArt = 0;
        #endregion
        #region ObtenerValores
        if (txtIngCantidad.Text != "")
        {
            cantidad = int.Parse(txtIngCantidad.Text);
        }
        else
        {
            cantidad = 0;
        }
        if (txtIngPU.Text != "")
        {
            preciounitario = double.Parse(txtIngPU.Text);
        }
        else
        {
            preciounitario = 0;
        }
        if (txtDescripcion.Text != "")
        {
            descripcion = txtDescripcion.Text;
        }
        else
        {
            descripcion = "";
        }
        if (cantidad >0 && preciounitario >0)
        {
            subtotalArt = cantidad * preciounitario;                
        }
        else
        {
            subtotalArt = 0;                    
        }
        #endregion
        #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 ObtenerMoneda
        ObtenerMoneda();
        if (MonedaID==1)
        {
            IVA = 0;
        }
        else
        {
            IVA = 0.16;
        }
        #endregion
        #region LLenarGrid
        if (Existe)
        {
            if (articulo != "" && cantidad > 0 && preciounitario > 0 && subtotalArt!=0 && descripcion != "")
            {
                DataRow drArticulosSeleccionados = dtMatrizArticulos.NewRow();
                foreach (DataRow drMatrizArticulos in dtMatrizArticulos.Rows)
                {
                    drArticulosSeleccionados["Articulo"] = articulo;
                    drArticulosSeleccionados["Cantidad"] = cantidad;
                    drArticulosSeleccionados["PrecioUnitario"] = Convert.ToDouble(preciounitario);
                    drArticulosSeleccionados["SubtotalArticulo"] = Convert.ToDouble(subtotalArt);
                    drArticulosSeleccionados["Detalle"] = Convert.ToString(descripcion);                        
                }
                dtMatrizArticulos.Rows.Add(drArticulosSeleccionados);
                subtotalGeneral += subtotalArt;
                if (subtotalGeneral > 0)
                {
                    txtIngSubtot.Text = string.Format("{0:n2}", (Math.Truncate(subtotalGeneral * 100) / 100));
                }
                else
                {
                    txtIngSubtot.Text = "";
                }
                if (IVA > 0)
                {
                    subtotalGlobal = subtotalGeneral * IVA;
                }
                else
                {
                    subtotalGlobal = subtotalGeneral;
                }
                if (IVA > 0 && subtotalGlobal > 0)
                {
                    totalGlobal = subtotalGeneral + subtotalGlobal;
                    txtIvaIngOrden.Text = string.Format("{0:n2}", (Math.Truncate(subtotalGlobal * 100) / 100));
                    txtIngTotal.Text = string.Format("{0:n2}", (Math.Truncate(totalGlobal * 100) / 100));
                }
                else
                {
                    txtIvaIngOrden.Text = string.Format("{0:n2}", (Math.Truncate(IVA * 100) / 100));
                    txtIngTotal.Text = "";
                    totalGlobal = subtotalGeneral;
                    txtIngTotal.Text = string.Format("{0:n2}", (Math.Truncate(totalGlobal * 100) / 100));
                }
            }
            else
            {
                if (articulo=="")
                {
                    objValidaciones.MostrarAviso("Por favor seleccione un artículo para agregar.",true,lblAvisoNOS);
                }
                if (descripcion=="")
                {
                    objValidaciones.MostrarAviso("Por favor ingrese los detalles de los artículos a agregar.", true, lblAvisoNOS);
                }
                if (subtotalArt==0)
                {
                    if (cantidad==0 && preciounitario==0)
                    {
                        objValidaciones.MostrarAviso("Por favor ingrese la cantidad y precio unitrio de los artículos a agregar.", true, lblAvisoNOS);
                    }
                    else
                    {
                        if (cantidad == 0)
                        {
                            objValidaciones.MostrarAviso("Por favor ingrese la cantidad artículos a agregar.", true, lblAvisoNOS);
                        }
                        if (preciounitario == 0)
                        {
                            objValidaciones.MostrarAviso("Por favor ingrese el precio unitario de los artículos a agregar.", true, lblAvisoNOS);
                        }
                    }
                }
            }
        }
        else
        {
            dtMatrizArticulos.Columns.Clear();
            dtMatrizArticulos.Rows.Clear();
            dtMatrizArticulos.Columns.Add("Articulo");
            dtMatrizArticulos.Columns.Add("Cantidad");
            dtMatrizArticulos.Columns.Add("PrecioUnitario");
            dtMatrizArticulos.Columns.Add("SubtotalArticulo");
            dtMatrizArticulos.Columns.Add("Detalle");
            if (articulo != "" && cantidad > 0 && preciounitario > 0 && subtotalArt != 0 && descripcion != "")
            {
                DataRow drArticulosSeleccionados = dtMatrizArticulos.NewRow();
                drArticulosSeleccionados["Articulo"] = articulo;
                drArticulosSeleccionados["Cantidad"] = cantidad;
                drArticulosSeleccionados["PrecioUnitario"] = Convert.ToDouble(preciounitario);
                drArticulosSeleccionados["SubtotalArticulo"] = Convert.ToDouble(subtotalArt);
                drArticulosSeleccionados["Detalle"] = Convert.ToString(descripcion);
                dtMatrizArticulos.Rows.Add(drArticulosSeleccionados);
                subtotalGeneral += subtotalArt;
                if (subtotalGeneral > 0)
                {
                    txtIngSubtot.Text = string.Format("{0:n2}", (Math.Truncate(subtotalGeneral * 100) / 100));
                }
                else
                {
                    txtIngSubtot.Text = "";
                }
                if (IVA > 0)
                {
                    subtotalGlobal = subtotalGeneral * IVA;
                }
                else
                {
                    subtotalGlobal = subtotalGeneral;
                }
                if (IVA >0 && subtotalGlobal > 0)
                {
                    totalGlobal = subtotalGeneral + subtotalGlobal;
                    txtIvaIngOrden.Text = string.Format("{0:n2}", (Math.Truncate(subtotalGlobal * 100) / 100));
                    txtIngTotal.Text = string.Format("{0:n2}", (Math.Truncate(totalGlobal * 100) / 100));
                }
                else
                {
                    txtIvaIngOrden.Text = string.Format("{0:n2}", (Math.Truncate(IVA * 100) / 100));
                    txtIngTotal.Text = "";
                    totalGlobal = subtotalGeneral;
                    txtIngTotal.Text = string.Format("{0:n2}", (Math.Truncate(totalGlobal * 100) / 100));
                }
            }
            else
            {
                if (articulo == "")
                {
                    objValidaciones.MostrarAviso("Por favor seleccione un artículo para agregar.", true, lblAvisoNOS);
                }
                if (descripcion == "")
                {
                    objValidaciones.MostrarAviso("Por favor ingrese los detalles de los artículos a agregar.", true, lblAvisoNOS);
                }
                if (subtotalArt == 0)
                {
                    if (cantidad == 0 && preciounitario == 0)
                    {
                        objValidaciones.MostrarAviso("Por favor ingrese la cantidad y precio unitrio de los artículos a agregar.", true, lblAvisoNOS);
                    }
                    else
                    {
                        if (cantidad == 0)
                        {
                            objValidaciones.MostrarAviso("Por favor ingrese la cantidad artículos a agregar.", true, lblAvisoNOS);
                        }
                        if (preciounitario == 0)
                        {
                            objValidaciones.MostrarAviso("Por favor ingrese el precio unitario de los artículos a agregar.", true, lblAvisoNOS);
                        }
                    }
                }
            }
        }
        #endregion           
        #region MuestraDatos
        dgvListArtIng.DataSource = dtMatrizArticulos;
        dgvListArtIng.Columns["PrecioUnitario"].ValueType = typeof(System.Decimal);//dgvListArtIng.Columns["PrecioUnitario"].ValueType = Type.GetType("System.Decimal")
        dgvListArtIng.Columns["PrecioUnitario"].DefaultCellStyle.Format = "##,##0.00";
        dgvListArtIng.Columns["SubtotalArticulo"].ValueType = typeof(System.Decimal);//dgvListArtIng.Columns["SubtotalArticulo"].ValueType = Type.GetType("System.Decimal")
        dgvListArtIng.Columns["SubtotalArticulo"].DefaultCellStyle.Format = "##,##0.00";
        dgvListArtIng.AllowUserToAddRows = false;
        #endregion
    }
    
asked by user66626 24.01.2018 в 18:05
source

2 answers

0

In the DatagridView property called DefautlCellStyle configure the Format to C2 .

That way the column will take the currency format.

I hope it will be helpful for future visits to this question.

    
answered by 06.03.2018 в 17:25
0

What is it to pass to string your variable (unit price, for example) to .ToString("N0") .

This is formatted to 10.000.000.

    
answered by 28.09.2018 в 16:41