版主,你好
又一次见面啦
这两天在研究FpSpread控件单元格Double数据类型校验问题
控件版本号:6.0.2002.2008
我是从RegExpCellType类继承下来,利用正则表达式检验double类型小数点后位数
代码如下:
namespace AmiFpSpread
{
[Serializable]
internal class ValidDoubleCellType : RegExpCellType
{
public ValidDoubleCellType(NumberFormatInfo nfi)
: base()
{
this.NumberFormat = nfi;
//设置ValidationExpression
if (this.NumberFormat != null && this.NumberFormat.NumberDecimalDigits == 0)
{
this.ValidationExpression = @"^(0|-?[1-9][0-9]*)$";
}
else
{
this.ValidationExpression = @"^(0|-?[1-9][0-9]*)$|^(0|-?[1-9][0-9]*)(.[0-9]{1," + this.NumberFormat.NumberDecimalDigits.ToString() + @"})?$";
}
//设置ErrorMessage
string format = "Doubleex, {0})";
switch (this.NumberFormat.NumberDecimalDigits)
{
case 0: base.ErrorMessage = string.Format(format, "1234"); break;
case 2: base.ErrorMessage = string.Format(format, string.Format("1234{0}56", this.NumberFormat.NumberDecimalSeparator)); break;
case 4: base.ErrorMessage = string.Format(format, string.Format("1234{0}5678", this.NumberFormat.NumberDecimalSeparator)); break;
default: base.ErrorMessage = string.Format(format, "1234.56,d"); break;
}
}
public override string ValidateEditorValue(object value)
{
string str = null;
if (value != null)
{
if (value.ToString().Length <= 0)
{
return str;
}
Regex regex = new Regex(this.ValidationExpression);
if (!regex.Match(value.ToString()).Success)
{
return this.ErrorMessage;
}
}
return str;
}
public override object Parse(string s)
{
if ((s == null) || (s.Length == 0))
{
return null;
}
try
{
NumberFormatInfo numberFormat = this.NumberFormat;
if (numberFormat == null)
{
return double.Parse(s, NumberStyles.Any);
}
return double.Parse(s, NumberStyles.Any, (IFormatProvider)numberFormat);
}
catch
{
return s;
}
}
public override string Format(object obj)
{
if ((obj == null) || (obj.ToString().Length == 0))
{
return string.Empty;
}
string str = null;
try
{
if (this.NumberFormat != null)
{
str = Convert.ToDouble(obj).ToString("N", this.NumberFormat) ?? "";
if (str == null || str.Length <= 0)
{
return str;
}
int num = 0;
int num2 = str.Length - 1;
int num3 = str.IndexOf(this.NumberFormat.NumberDecimalSeparator);
if (num3 >= 0)
{
num2 = num3 + this.NumberFormat.NumberDecimalDigits;
while ((num2 > num3) && (str[num2] == '0'))
{
num2--;
num++;
}
}
if ((num == this.NumberFormat.NumberDecimalDigits) && (num > 0))
{
num2--;
num++;
}
return (str.Substring(0, num2 + 1) + str.Substring((num2 + 1) + num));
}
return obj.ToString();
}
catch
{
return obj.ToString();
}
}
public override bool Deserialize(System.Xml.XmlNodeReader r)
{
return true;
}
}
}
页面查看时第一次运行效果正常
第二次运行会报错
报错信息如下:
1.jpg
(162.76 KB, 下载次数: 293)
|
|