Question

Alain Dekker, 2 on Wed, 28 Nov 2012 09:55:35


I've got a System.Windows.Forms.DataGridView with a bunch of columns displaying data from a specific table from a SQL database. Is there any way to control the formatting of decimal places in the grid? I've added the value "123.57" and it displays like that, but when I add "135.00" it shows as "135".

Thanks,
Alain


Sponsored



Replies

Pieter Geerkens on Wed, 28 Nov 2012 10:08:37


Try this:

dataGridView.Columns[2]
   .DefaultCellStyle.Format = "N2";

More info here:

http://msdn.microsoft.com/en-us/library/f9x2790s.aspx

Alain Dekker, 2 on Wed, 28 Nov 2012 15:25:45


Great, thanks that works. I changed the default formatting to "N2" and now all floating point columns have the required number of decimal places, brilliant!

Quick question: I don't actually add or remove columns directly, I simply set the "DataSource" parameter of the grid view. Is there any way to manipulate the formatting of individual columns (perhaps based on fore-knowledge of the table columns when the table selection is made)?

Thanks,
Alain

<Pieter Geerkens> wrote in message news:fb528d88-4015-4aec-8b97-ed123b1298e6@communitybridge.codeplex.com...

Try this:

dataGridView.Columns[2]
    .DefaultCellStyle.Format = "N2";

More info here:

http://msdn.microsoft.com/en-us/library/f9x2790s.aspx

<http://msdn.microsoft.com/en-us/library/f9x2790s.aspx>


"Premature optimization is the root of all evil." - Knuth

If I provoked thought, please click the green arrow

If I provoked Aha! please click Propose as Answer

Lisa Zhu on Thu, 29 Nov 2012 08:21:46


Quick question: I don't actually add or remove columns directly, I simply set the "DataSource" parameter of the grid view. Is there any way to manipulate the formatting of individual columns (perhaps based on fore-knowledge of the table columns when the table selection is made)?

Hi Alain,

Pieter's suggestion is work for individual columns.

dataGridView.Columns[2].DefaultCellStyle.Format = "N2";

This code just make sure the third (index start form 0) column contains two  decimal places.

Regards,