0

I have a number pad with buttons containing text from 0-9 and a button to add a decimal point "." and when you click the button, it adds the text to the datagridview cell. However, is there any way to limit the input so that you cannot add more than two numbers after the decimal point (as the datagridview column represents the price)?

Something like a rule in the button_click event

if (ddr.Contains(".") && ddr.EndsWith(".##"))
Icecubelegacy
  • 189
  • 1
  • 4
  • 14

2 Answers2

0

May be you can try something like this

        string[] ddrs = ddr.Split('.');
        if(ddr.Contains(".") && ddrs[1].Length == 2)
        {

        }
Mohit S
  • 13,723
  • 6
  • 34
  • 69
0

Before assigning the values to gridview cell change the format to this:

string valuetobeassigned = ddr.ToString("0.##"); 

note : please refer some sources if you want different formatting

force a string to 2 decimal places

http://www.daveoncsharp.com/2009/09/formatting-decimals-in-csharp/

Using String Format to show decimal upto 2 places or simple integer

Community
  • 1
  • 1
Dirty Developer
  • 551
  • 5
  • 22