Alexandre
Alexandre

Reputation: 2025

DatagridView with money format

I want to create a money mask for one cell in datagridview, I have tried a lot of things, when I find in the internet, but, anyone worked :(

dataGridView1.Columns["Valor"].DefaultCellStyle.Format = "#.0,00;(#.0,00)";

or

var provider = (CultureInfo)CultureInfo.CurrentCulture.Clone();
provider.NumberFormat.CurrencySymbol = "";
dataGridView1.Columns["Valor"].DefaultCellStyle.FormatProvider = provider;
dataGridView1.Columns["Valor"].DefaultCellStyle.Format = "C";

The second option is very more good, because, here in Brazil we use comma like decimal-point..., but, when I put that after InitializeComponent();, the system not work :(, the cell just make a simple splash and any more.....

Upvotes: 0

Views: 5094

Answers (1)

Andrew Paes
Andrew Paes

Reputation: 2012

Try this one:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

    namespace Test
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                DataSet dataSet1 = new DataSet("dataSet");
                DataTable dataTable1 = dataSet1.Tables.Add("Table");
                DataColumn dataColumn1 = dataTable1.Columns.Add("Valor", typeof(decimal));
                dataTable1.PrimaryKey = new DataColumn[] { dataColumn1 };

                DataRow dRow = dataSet1.Tables["Table"].NewRow();
                dRow["Valor"] = Convert.ToDecimal("1985,56");
                dataSet1.Tables["Table"].Rows.Add(dRow);

                dRow = dataSet1.Tables["Table"].NewRow();
                dRow["Valor"] = Convert.ToDecimal("85,756");
                dataSet1.Tables["Table"].Rows.Add(dRow);

                dRow = dataSet1.Tables["Table"].NewRow();
                dRow["Valor"] = Convert.ToDecimal("145,6");
                dataSet1.Tables["Table"].Rows.Add(dRow);

                InitializeComponent();

                if (dataSet1 != null && dataSet1.Tables.Count > 0 && dataSet1.Tables[0] != null && dataSet1.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dataRowTemp in dataSet1.Tables[0].Rows)
                    {
                        Debug.Print(dataRowTemp["Valor"].ToString());
                    }

                    dataGridView1.DataSource = dataTable1;
                }

                //var provider = (CultureInfo)CultureInfo.CurrentCulture.Clone();
                var provider = new System.Globalization.CultureInfo("pt-BR");
                //var provider = new System.Globalization.CultureInfo("en");

                dataGridView1.Columns["Valor"].DefaultCellStyle.FormatProvider = provider;
                dataGridView1.Columns["Valor"].DefaultCellStyle.Format = "C2";
                dataGridView1.Columns["Valor"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            }
        }
    }

Upvotes: 1

Related Questions