Currency Culture Formatting not applying on DataGridView Column

风流意气都作罢 提交于 2019-12-08 02:27:19

问题


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 ..


回答1:


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 :)




回答2:


(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");


来源:https://stackoverflow.com/questions/14029542/currency-culture-formatting-not-applying-on-datagridview-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!