How to format numbers to 3 decimal place in datagridview using vb.net

自作多情 提交于 2019-12-10 12:57:53

问题


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.

DataGridView1.Columns(3).DefaultCellStyle.Format = "#.###" 

回答1:


Do you try with this one?

DataGridView1.Columns(2).DefaultCellStyle.Format = "N3"

Also this one may helpful:

  • Format Datagridview columns to numeric



回答2:


Try it

DataGridView1.Sort(DataGridView1.Columns(2), System.ComponentModel.ListSortDirection.Ascending)

or

DataGridView1.Sort(DataGridView1.Columns(2), System.ComponentModel.ListSortDirection.Descending)



回答3:


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



回答4:


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

hope it can help :)



来源:https://stackoverflow.com/questions/20053128/how-to-format-numbers-to-3-decimal-place-in-datagridview-using-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!