如何使用 vb.net 在 datagridview 中将数字格式化为小数点后 3 位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20053128/
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
How to format numbers to 3 decimal place in datagridview using vb.net
提问by stackexchange12
I have a vb.net windows forms application that is using a datagridview. I'm hoping to find a simple way to format certain datagridview cells numbers upto 3 decimal places. This is what I have done so far, but it doesn't seem to format everything correctly.
我有一个使用 datagridview 的 vb.net windows 窗体应用程序。我希望找到一种简单的方法来将某些 datagridview 单元格编号格式化为小数点后 3 位。这是我到目前为止所做的,但它似乎没有正确格式化所有内容。
DataGridView1.Columns(3).DefaultCellStyle.Format = "#.###"
回答by ridoy
Do you try with this one?
你试试这个?
DataGridView1.Columns(2).DefaultCellStyle.Format = "N3"
Also this one may helpful:
这也可能有帮助:
回答by Agung Wibowo
Try it
尝试一下
DataGridView1.Sort(DataGridView1.Columns(2), System.ComponentModel.ListSortDirection.Ascending)
or
或者
DataGridView1.Sort(DataGridView1.Columns(2), System.ComponentModel.ListSortDirection.Descending)
回答by user6539023
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.DataGridView1.Columns.Add("TEST", "TEST")
DataGridView1.Columns("TEST").DefaultCellStyle.Format = "N2"
DataGridView1.Columns("TEST").ValueType = GetType(Decimal)
End Sub
回答by Peterboy
I have the same issue too. but my code is:
我也有同样的问题。但我的代码是:
for R = 0 to DataGridView1.rows.count-1
DataGridView1.rows(r).cells(3).value=math.round(DataGridView1.rows(r).cells(3).value,2)
Next
R
is for the Currentrow of DataGridView1 3 is your Column
i used to round of the datagridview cells in "2" decimal places and i fixed the problem :)
R
用于 DataGridView1 的 Currentrow 3 是你的列,我用来将 datagridview 单元格四舍五入到“2”个小数位,我解决了这个问题:)
hope it can help :)
希望它可以帮助:)