Ahmad
Ahmad

Reputation: 13406

Currency Culture Formatting not applying on DataGridView Column

I have 2 DataGridViews (DGV), and in both I have currency columns which I want to format. The code I'm writing seems to work in one, but not in the other.

Both the DGV's are set up this way: Data is first loaded into a DataTable. A BindingSource then links to this DataTable. And lastly the DGV's use this BindingSource object for their data.

I use the following code in the form's Load event to customize both DGVs' currency columns:

dataGridView.Columns[columnIndexHere].DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("de-DE");
dataGridView.Columns[columnIndexHere].DefaultCellStyle.Format = String.Format("c")

Seems to format the columns in one DGV both not the other. Also, the DGV in which its NOT working, doesn't even have that much functionality code in its parent Form .. I have checked the code, and the DGV's properties, to see if I'm changing the formatting somewhere else, but I haven't found anything ..

The ONLY slight difference between the way the two DGV's load data is that, for the first DGV's DataTable (in which formatting works), data is loaded thru a SELECT statement (via my own functions) ... In the second DGV's DataTable (for which formatting is NOT working), I don't load any data from DB, and instead just manually define the columns (in the DataTable), because in this scenario the user needs to input the data ..

Any clues why formatting not working on the second DGV ?


EDIT: Adding More Information:

To demonstrate the problem, I've created a new C# winforms project, added just one DataGridView on it, and added the following code to the Load event. Even for this code, the currency formatting is NOT being done:

DataTable dataTable = new DataTable();
dataTable.Columns.Add("ColA");
dataTable.Columns.Add("ColB");
dataTable.Columns.Add("ColC");


dataTable.Rows.Add("John", 832.293, "London");
dataTable.Rows.Add("Alice", 32972978.20489, "Atlanta");
dataTable.Rows.Add("Mark", 9184793284739, "Tokyo");

BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = dataTable;

dataGridView1.DataSource = bindingSource;

var format = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
format.CurrencySymbol = "Mn. ";
dataGridView1.Columns["ColB"].DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("de-DE");
dataGridView1.Columns["ColB"].DefaultCellStyle.Format = String.Format("c");

Hope this helps in diagnosing the problem more ..

Upvotes: 2

Views: 7700

Answers (2)

André
André

Reputation: 4497

(answer previously from the question)

Added the following line after defining the two columns, and now it works:

qBDFrmDTForDisplay.Columns["Combined Price"].DataType = Type.GetType("System.Decimal");

Upvotes: 1

spajce
spajce

Reputation: 7092

you don't need a BindingSource to list all rows, just

dataGridView1.DataSource = dataTable,

and since you're using a DataTable, remove this part

dataGridView1.Columns["ColB"].DefaultCellStyle.Format = String.Format("c");

and try to use the CellFormatting event.

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 1) //Column ColB
            {
                if (e.Value != null)
                {
                    e.CellStyle.Format = "c";
                }
            }
        }

I hope it will help :)

Upvotes: 2

Related Questions