本文介绍了限制小数点并将数字更正为DataGrid视图中的2个小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面给出的代码创建了用于验证文本框的代码,以纠正到小数点后2位.

I created Codes for Validating Textbox Correct to 2 decimal Places using the code given below.

private void txtAge_KeyPress(object sender, KeyPressEventArgs e)
        {
            string str = txtAge.Text;                               //  User Control
            int len = str.Length;
            int index = 0;
            int dcml = 2;                                           // 2 indeicates the Deccimal Points Needed   (User Defined)
            if (str.Contains('.'))
            {
                index = str.IndexOf('.');
                index = len - index;
            }
            if (Char.IsControl(e.KeyChar) == false)
            {
                if (((Char.IsDigit(e.KeyChar)) && (index <= dcml)) || ((e.KeyChar == '.') && (!(str.Contains('.')))))
                {
                }
                else
                {
                    e.Handled = true;
                }
            }
        }




但我无法使用此代码在数据网格视图"中更正文本验证,字符串str并未从datagridview单元中获取值.它仅获取空值.但是它仅接受数字和小数点.我可以输入任意数量的小数点和小数点后的任何数字.我该如何更改,将值限制为2个小数位,并且只允许一个小数点.




but I cannot use this code to Correct the Text Validation in Data Grid View, string str is not taking the value from datagridview cell. it gets only the null values. but It accepts only digits and decimal points. I can enter any number of decimal points and any numbers after decimal point. How can i change this, restricting the value to 2 decimal places and allow only one decimal point.

推荐答案


这篇关于限制小数点并将数字更正为DataGrid视图中的2个小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 11:35