货币文化格式化不适用于DataGridView列 [英] Currency Culture Formatting not applying on DataGridView Column

查看:132
本文介绍了货币文化格式化不适用于DataGridView列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个DataGridViews(DGV),而且我都有我要格式化的货币列。我写的代码似乎在一个工作,但不是在另一个。



DGV都是这样设置的:数据首先加载到DataTable中。 BindingSource然后链接到这个DataTable。最后,DGV使用这个BindingSource对象的数据。



我在表单的Load事件中使用以下代码来自定义DGV的货币列:

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

似乎格式化一个DGV中的列不是另一个。另外,其中它不工作的DGV,甚至没有其父表单中的功能代码。我检查了代码和DGV的属性,看看我是否在其他地方更改格式,但是没有找到任何东西..



两个DGV的加载数据之间只有一点微不足道的是,对于第一个DGV的DataTable(格式化工作),数据通过一个SELECT语句(通过我自己的函数)加载...在第二个DGV的DataTable(其格式不工作)中,我不从DB加载任何数据,而只是手动定义列(在DataTable中),因为在这种情况下,用户需要输入数据。



任何线索为什么格式化不能在第二个DGV上工作?






编辑:添加更多信息:



为了演示问题,我创建了一个新的C#winforms项目,只添加了一个DataGridView,并添加了它以下代码到Load事件。即使是这个代码,货币格式化还没有完成:

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

希望这有助于更好地诊断问题。



编辑2 :找到解决方案!



在定义两列后添加以下行,现在可以使用: p>

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

解决方案

你不需要一个 BindingSource 列出所有的行,只是



dataGridView1.DataSource = dataTable



由于您使用的是 DataTable ,请删除此部分



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



并尝试使用 CellFormatting event。

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

我希望它会有所帮助: )


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

EDIT 2: Found the solution !

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

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

解决方案

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

这篇关于货币文化格式化不适用于DataGridView列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆