呵呵,自动计算的年龄需要具体到 年月 日 比如今天是2012-05-22,在1982-05-22出生的年龄为30岁,在
1982-05-23出生的年龄为29岁;
后面找了几个狐狸爸爸之前回复的例子,终于可以了,把代码发布出来跟大家一起分享一下。
If e .DataCol . Name = "身份证号码" Then '如果更改的是身份证号码列
If e .DataRow . IsNull( "身份证号码" ) Then '身份证号码是否为空
e .DataRow ( "出生日期" ) = Nothing '如果为空,则清除出生日期
e .DataRow ( "性别") = Nothing
Else
'否则从身份证号码列中提取出生日期
e .DataRow ( "出生日期" ) = ReadBirthday ( e. DataRow ("身份证号码" ))
e .DataRow ( "性别") = ReadSex (e .DataRow ( "身份证号码" ))
End If
End If
If e.DataCol.name = "出生日期" Then '如果更改的是出生日期列
If e.DataRow.IsNull("出生日期") Then '出生日期是否为空
e.DataRow("年龄") = Nothing '如果为空,则清除年龄
Else
'否则从出生日期列中提取出年龄
Dim n As Integer= Date.Today.Year - e.DataRow("出生日期").Year
If e.DataRow("出生日期").AddMonths(n*12) > Date.Today Then
n = n -1
End If
e.DataRow("年龄") = n
End If
End If