vb.net 格式化或填充 DataGridView 控件中的数字列 (DefaultCellStyle.Format)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15437373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:49:18  来源:igfitidea点击:

Format or pad a number column in DataGridView control (DefaultCellStyle.Format)

vb.netwinformsdatagridview

提问by Our Man in Bananas

I have a DatagridView control on my form.

我的表单上有一个 DatagridView 控件。

The first column is a Record ID which can be between 3 and 5 digits on the db, but should be formatted on the front end as 5 digits with padded zeros/leading zeros.

第一列是记录 ID,它可以在数据库上的 3 到 5 位数字之间,但应在前端格式化为 5 位数字,并带有填充零/前导零。

So it looks like 901and I want it to look like 00901

所以它看起来像901而我希望它看起来像00901

I looked at

我在看

and

and I tried:

我试过:

dgvDeletedRecords.Columns(0).DefaultCellStyle.Format = "00000"

in my form load event after the data is loaded into the grid

将数据加载到网格后,在我的表单加载事件中

But it doesn't work.

但它不起作用。

Where am I going wrong please?

请问我哪里错了?

采纳答案by Fabio

Try this number format "#####00000" Code for testing I had used:

试试这个数字格式“#####00000”我用过的测试代码:

Me.RichTextBox.Text &= String.Format("{0:#####00000}",123)
Me.RichTextBox.Text &= Environment.NewLine
Me.RichTextBox.Text &= String.Format("{0:#####00000}",12345)

Tested in RichTextBox, but sure a same format will working for DefaultCellStyle.Format

在 RichTextBox 中测试,但确定相同的格式适用于 DefaultCellStyle.Format

 dgvDeletedRecord.Columns(0).DefaultCellStyle.Format = "#####00000"

In format sting "#"-sign tell how mush number will be show(prefix zeros not shown) Then "0"-sign tell how mush zeros will be shown, and here prefix zeros will be showed

在格式字符串中“#”-符号告诉将如何显示糊状数字(未显示前缀零)然后“0”-符号告诉将如何显示糊状零,此处将显示前缀零

MSDN Number Formats

MSDN 数字格式

回答by DiskJunky

try the following;

尝试以下方法;

dgvDeletedRecord.Columns(0).DefaultCellStyle.Format = "{0:N4}"