שתף באמצעות


How to Set Number Formatting to Datagrid View?

Question

Tuesday, November 15, 2011 12:52 PM

How do you set datagridview formatting to:

From 1000 to 1,000.00

I've tryed

 Me.DataGridView1.Columns(8).DefaultCellStyle.Format = "##,0"
        Me.DataGridView1.Columns("ORDERQUANT").DefaultCellStyle.Format = "##,0"

This is the an example i found that i also dosen't work for me.

 Private Sub SetFormatting()
        With Me.dataGridView1
            .Columns("UnitPrice").DefaultCellStyle.Format = "Currency"
            .Columns("ShipDate").DefaultCellStyle.Format = "d"
            .Columns("CustomerName").DefaultCellStyle.Alignment = _
                DataGridViewContentAlignment.MiddleRight
            .DefaultCellStyle.NullValue = "no entry"
            .DefaultCellStyle.WrapMode = DataGridViewTriState.True
        End With
    End Sub

 

but with Little success of setting teh format even to 1,000 from 1000.

 

any advice would be excellent, thanks in advance
 

All replies (5)

Tuesday, November 15, 2011 1:51 PM ✅Answered | 1 vote

Hi,

try using the "N" format specifier. Take a look at: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#NFormatString

Hannes

If you have got questions about this, just ask.

In a perfect world,
users would never enter data in the wrong form,
files they choose to open would always exist
and code would never have bugs.

C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/


Tuesday, November 15, 2011 2:30 PM ✅Answered

You can directly specify a format, at design time, in the properties of the column of the datagriview.

I use a lot the "N2" format.


Tuesday, November 15, 2011 1:04 PM

I beleieve the last time I had this issue it was caused by the data type of the column itself.  In other words, if your column in type String then the numeric formatting will be ignored.  Can you confirm that the column is a numeric type (i.e. decimal, integer)?


Tuesday, November 15, 2011 1:46 PM

I can comfirm the column is set to decimal.

this is how the column is set-up

        col8.DataType = System.Type.GetType("System.Decimal")
        col8.ColumnName = "ORDERQUANT"
        col8.Caption = "Order Quantity"
        col8.ReadOnly = False
        Me.DataGridView1.Columns("ORDERQUANT").DefaultCellStyle.Format = "##,0"

It also dosen't format on integer aswell. but i would prefer decimal as the format i'm going for is 0,000,000.00 somthing like that.

 

 


Tuesday, November 15, 2011 2:23 PM

Hi,

try using the "N" format specifier. Take a look at: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#NFormatString

Hannes

If you have got questions about this, just ask.

In a perfect world,
users would never enter data in the wrong form,
files they choose to open would always exist
and code would never have bugs.

C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

This is a excellent, but i can't seem to attach it to the Column within the .DefaultCellStyle.Format

These are the two i have tryed with your N value for the format.

Me.DataGridView1.Columns("ORDERQUANT").DefaultCellStyle.Format = ("N",  CultureInfo.InvariantCulture)

Me.DataGridView1.Columns("ORDERQUANT").DefaultCellStyle.Format = "N"

I've tryed the Orignal code and it works Super with a value before you format the cell. but i would like it to be attached to the cell as this is far easier for me to use. i think i'm having a perticually 'thick' day.

 

If you have any examples that would be amazing. Thanks in advance