DataGridViewCellStyle 显示以千为单位的数值数据

标签 datagridview formatting datagridviewcellstyle

我只是想知道是否有一种方法可以在 DataGridViewCellStyle 中创建自定义格式来显示数千个数据。

在 Excel 中有一种自定义格式,您可以在单元格中键入 1000,但它显示为 1。我尝试搜索这些板,但我所有的搜索都返回如何插入千个分隔符,我已经知道如何插入要做的事。

我当前的解决方法是在加载 DGV 之前将数据除以 1000,然后将返回的用户输入乘以 1000,但必须有更好的方法。我正在处理一些非常大的数字(以智利比索计算,不少于),所以我无法显示真实的数字。

最佳答案

使用格式字符串“0”

From MSDN :

Number scaling specifier: If one or more ',' characters is specified immediately to the left of the explicit or implicit decimal point, the number to be formatted is divided by 1000 each time a number scaling specifier occurs

在 RichTextBox 中,这种格式对我有用:

richTextBox.Text = String.Format("{0:0,}", 24000);//Show as 24

在 DataGridView 中,可以在设计器中或通过代码使用相同的格式:

//Number format "0,.###" will show number 12230 as 12.23

this.datagridview.Columns[this.dgvYuorColumn.Name].DefaultCellStyle.Format = "0,.###";

关于DataGridViewCellStyle 显示以千为单位的数值数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15668897/

相关文章:

c# - 单击后台表单时,Datagridview 将消失

C# DataGridViewLinkCell 显示

c# 将 DataGridView 保存到 Xml 文件

vb.net - 重载DataGridViewCellStyle并给出默认值

powershell - 无法为 DataGridView 中的特定行着色

css - 在 DIV 中对齐 3 个文本(左、中、右)

php - 当我们有 2 个或更多选项时如何在 php 中格式化文本

python - python中print语句中第二个括号[]的含义

DataGridView:使用小数的 DefaultCellStyle