7

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 = "#.###" 
ridoy
  • 6,274
  • 2
  • 29
  • 60
stackexchange12
  • 622
  • 9
  • 20
  • 41
  • What do you mean by "it doesn't seem to format everything correctly."? What doesn't it format correctly? – rory.ap Nov 18 '13 at 17:02
  • If I use this to format the entire datagridview as a whole it seems to work fine. However one of my column indices seems to be problematic and it won't format correctly. Meaning that it still has decimals beyond the thousandths place. – stackexchange12 Nov 18 '13 at 17:21
  • The code is correct. A simple mistake would be to put a string in the cell instead of a value of type Double or Decimal. – Hans Passant Nov 18 '13 at 18:53
  • I had similar issue and the root cause was a forgotten routine which changed the format after I had set it. So if this does not work, check your code for further calls that change the format. It will work on populated datagridviews. – Rob Jul 06 '17 at 12:52

5 Answers5

21

Do you try with this one?

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

Also this one may helpful:

ridoy
  • 6,274
  • 2
  • 29
  • 60
  • Format strings are described here: https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx – Mattia72 Feb 09 '17 at 07:29
0

Try it

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

or

DataGridView1.Sort(DataGridView1.Columns(2), System.ComponentModel.ListSortDirection.Descending)
AlexB
  • 7,302
  • 12
  • 56
  • 74
0
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
0

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

Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
Peterboy
  • 1
  • 1
0

Just replace:

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

by:

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

where "N3" means numeric data with three decimals. If all columns are to be "N3", you can do it at once with:

DataGridView1.DefaultCellStyle.Format = "N3"

you can also apply a general cell alignment, to all cells in the data grid, for instance:

DataGridView1.DefaulCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

Finally, you can also define general (all columns) numeric format at DataGridView properties window, with Format option under DataGridViewCellStyle box.

And as you can see, there is no need for loops here while the style definitions are not specific.

ePuntel
  • 91
  • 3