如何格式化数据表列 [英] How to format datatable column

查看:68
本文介绍了如何格式化数据表列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表,其中包含以下数值。



OA OA_Date OA_currency OA_Balance_Amount OA_remarks

39392 08-Feb-16 USD -573518.00两次已付款 - US410-0001067399

39402 08-Feb-16 USD -416612.30超额付款 - US410-0001123424

39385 08-Feb-16 EUR -303531.76需要详细信息


我的要求是我需要金额列号以逗号表示,即-573518.00到-573,518.00。



所以我的数据表必须是这样的



OA OA_Date OA_currency OA_Balance_Amount OA_remarks

39392 08-Feb-16 USD -573,518.00两次付费-US410 -0001067399

39402 08-Feb-16 USD -416,612.30超额付款 - US410-0001123424

39385 08-Feb-16 EUR -303,531.76需要详细信息



是否有可能实现这一目标。请帮忙。

提前致谢。



我的尝试:



i检查了很多网站,但我没有发现任何符合我标准的网站。

解决方案

你没有格式化DataTable - 格式化源代码(通常是数据库)或更多通常格式化表示控件:GridView,DataGridView或显示值的类似。

你提到的逗号不是真实的 - 它们是用户文化中的演示文稿,可能根本不是逗号,或者可能位于不同的数字位置。

例如,英国数字可以分成数千:1,234,567,890.1,每三位数之间用逗号分隔。然而印度的等价物将基于十万分之一,并将前三个数字分开,然后分别为两个数字:1,23,45,67,890.1

其他文化交换逗号和点。有些用户根本不想要逗号分隔!

但是尝试格式化DataTable,你会丢弃数字本身,并用字符串替换它 - 这意味着无法再应用本地格式在逐个用户的基础上。



将它留在表示层,如果你真的必须修复那里的格式(但我不建议它!)


除了解决方案1 ​​



格式化数字时,您可以确保它获取本地文化显示如下:

 Console.WriteLine(aNumber.ToString(  N)); 



或特定文化(我重复OriginalGriff的话 - 我不推荐它)例如for Gujarati

 Console.WriteLine(aNumber.ToString(  N,CultureInfo.CreateSpecificCulture(  gu-IN))); 



类似地,如果您在DataGridView中显示数据,那么您可以格式化相应的列,如下所示

 dataGridView1.DataSource = dt; 
dataGridView1.Columns [ 3 ]。DefaultCellStyle.Format = N;



如果你想强制显示文化(不推荐),那么添加这行

 dataGridView1.Columns [ 3 ]。DefaultCellStyle.FormatProvider =  new  CultureInfo(  gu-IN); 


格式化a的示例你想要的数字:

  decimal  d =  199999  .99M; 
string s = d.ToString( C2 new System.Globalization.CultureInfo( en-US));



这将给出结果

 

I have a datatable with following values.

OA OA_Date OA_currency OA_Balance_Amount OA_remarks
39392 08-Feb-16 USD -573518.00 Twice paid-US410-0001067399
39402 08-Feb-16 USD -416612.30 Excess paid-US410-0001123424
39385 08-Feb-16 EUR -303531.76 Details required

My requirement is i need amount column number to be in commas, that is -573518.00 to -573,518.00.

So my data table must be like this

OA OA_Date OA_currency OA_Balance_Amount OA_remarks
39392 08-Feb-16 USD -573,518.00 Twice paid-US410-0001067399
39402 08-Feb-16 USD -416,612.30 Excess paid-US410-0001123424
39385 08-Feb-16 EUR -303,531.76 Details required

Is it possible to achieve this. Please help.
Thanks in advance.

What I have tried:

i had checked many sites but i didn't found anything that fulfills my criteria.

解决方案

You don't format a DataTable - you format the source (normally a DB) or more usually you format the presentation control: the GridView, DataGridView, or similar that display the values.
The commas you mention aren't "real" - they are an artifact of the presentation in the users culture, and may not be commas at all, or may be in a different digit position.
For example, UK numbers can be split into thousands: 1,234,567,890.1 with a comma between each three digits. However the Indian equivalent would be based on the lakh, and separate the first three digits, then each two: 1,23,45,67,890.1
Other cultures swap the comma and dot. Some users don't want comma separation at all!
But trying to format the DataTable, you throw away the number itself, and replace it with a string - which means that local formatting can no longer be applied on a user-by-user basis.

Leave it to the presentation layer, and "fix" the formatting there if you really must (but I don't recommend it!)


In addition to Solution 1

When you format the number you can ensure it picks up the local culture display as follows:

Console.WriteLine(aNumber.ToString("N"));


Or to a specific culture (I repeat OriginalGriff's words - I don't recommend it) e.g. for Gujarati

Console.WriteLine(aNumber.ToString("N", CultureInfo.CreateSpecificCulture("gu-IN")));


Similarly if you are displaying the data in a DataGridView then you can format the appropriate column as follows

dataGridView1.DataSource = dt;
dataGridView1.Columns[3].DefaultCellStyle.Format = "N";


And if you want to force the display culture (Not recommended) then add this line

dataGridView1.Columns[3].DefaultCellStyle.FormatProvider = new CultureInfo("gu-IN");


Example to format a number as you want it:

decimal d = 199999.99M;
string s = d.ToString("C2", new System.Globalization.CultureInfo("en-US"));


This will give the result


这篇关于如何格式化数据表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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