问题
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