c# - How to format a column with number decimal with max and min in DataGridView? -


i want format decimal places displayed , captured in datagridview, have minimum number of decimal places , maximum number of decimal places. example:

if caught, "120.0" display "120.00" if caught "120.01" display "120.01" if caught "120,123" display "120,123" if caught "120.1234" display "120.1234" if caught "120.12348" display "120.1235" (round) 

in datagridview column "txtcdecimal" has property (from designer)

txtcdecimal.defaultcellstyle.format = "n2";  txtcdecimal.defaultcellstyle.format = "0.00##";  // answer. not work event interfered 

the mask "0.00##" work "n2" 2 decimals correct rounding 2 decimal places not need (as show in example)

how can in simple manner without consuming many resources?

thanks harlam357 & tom garske

to format between 2 , 4 decimal places can use custom format string.

txtcdecimal.defaultcellstyle.format = "0.00##" 

to go bit further...

public partial class form1 {    public form1()    {       initializecomponent();        var list = new list<data>();       list.add(new data { prop1 = 1, prop2 = 1.2 });       list.add(new data { prop1 = 2, prop2 = 1.234567 });        datagridview1.columns.add("prop1", "prop1");       datagridview1.columns["prop1"].datapropertyname = "prop1";       datagridview1.columns.add("prop2", "prop2");       datagridview1.columns["prop2"].datapropertyname = "prop2";       datagridview1.columns["prop2"].defaultcellstyle.format = "0.00##";       datagridview1.datasource = list;    }     class data    {       public int prop1 { get; set; }       public double prop2 { get; set; }    } } 

Comments

Popular posts from this blog

Glib GIOChannel -

delphi - When I encode/decode SMS PDU (GSM 7 Bit) user data, do I need prepend the UDH first? -

python - Obscure curses error message when creating a sub window -