Another modifier for the datagridview column.Format() property?

  • Thread starter Thread starter Earl
  • Start date Start date
E

Earl

Somewhat of a trivial thing, but I have not seen this either on Google or in
the documentation: Is it possible to apply another modifier to the Format
property of a datagridview column?

I've got some expression columns that calculate decimal values which may end
up like "4.333333333". I'd like to round that down to 3 decimal places, but
you cannot use Round in your expression columns. (Incidentally, I'm not
interested in adding two more columns to the data coming across with the SQL
just in order to round it -- I'd rather keep the expression columns.)

If you manually add columns to a dgv, you can set the number of decimal
places, so it would seem that there is a property available to do this
programmatically.

Does anyone know if there another modifier? Perhaps something like
dgv.Columns(0).DefaultCellStyle.Format = "d, 3" (which does not have any
affect)?
 
Earl,

You're on the right track. In your format, you can specifiy an N length for
numeric formats.

{0:Cn} Currency. Displays numeric values in currency format with a leading
dollar sign; n indicates the number of decimal places. If n is omitted, the
default currency precision is two decimal digits.
{0:Dn} Decimal. Displays integer values; n indicates the minimum number of
digits desired in the resulting string. If necessary, the number is padded
with zeros to its left to produce the number of digits given by the
precision specifier.
{0:Fn} Fixed-point. Displays numeric values in fixed format; n indicates the
desired number of decimal places.
{0:Nn} Number. The number is converted to a string; n indicates the desired
number of decimal places. Commas are inserted between each group of three
digits to the left of the decimal point.
{0:Pn} Percent. Displays numeric values in percentage format; n indicates
the desired number of decimal places.
(from
http://msconline.maconstate.edu/Tutorials/ASPNET2/ASPNET07/aspnet07-01.aspx)

You can then specify this programatically, using:

dgv.Columns(0).DefaultCellStyle.Format = "D3"

Hope this helps,


Steve
 
Fantastic, just what I was looking for. Thanks.

PlatinumBay said:
Earl,

You're on the right track. In your format, you can specifiy an N length
for numeric formats.

{0:Cn} Currency. Displays numeric values in currency format with a leading
dollar sign; n indicates the number of decimal places. If n is omitted,
the default currency precision is two decimal digits.
{0:Dn} Decimal. Displays integer values; n indicates the minimum number of
digits desired in the resulting string. If necessary, the number is padded
with zeros to its left to produce the number of digits given by the
precision specifier.
{0:Fn} Fixed-point. Displays numeric values in fixed format; n indicates
the desired number of decimal places.
{0:Nn} Number. The number is converted to a string; n indicates the
desired number of decimal places. Commas are inserted between each group
of three digits to the left of the decimal point.
{0:Pn} Percent. Displays numeric values in percentage format; n indicates
the desired number of decimal places.
(from
http://msconline.maconstate.edu/Tutorials/ASPNET2/ASPNET07/aspnet07-01.aspx)

You can then specify this programatically, using:

dgv.Columns(0).DefaultCellStyle.Format = "D3"

Hope this helps,


Steve
 
Back
Top