1 2 下一页 Required、Precision、MaxValue、MinValue: begin { Required: 必填字段 } with TIntegerField.Create(Self) do begin FieldName := ''ID''; Required := True; DataSet := ClientDataSet1; end; { Precision: 浮点数精度} with TFloatField.Create(Self) do begin FieldName := ''Float''; Precision := 3; { 譬如: 输入 1.2345 只会保留 1.23 } DataSet := ClientDataSet1; end; { MaxValue、MinValue } with TIntegerField.Create(Self) do begin FieldName := ''Integer''; MinValue := 1; MaxValue := 99; DataSet := ClientDataSet1; end; ClientDataSet1.CreateDataSet; end;
可在字段的 CustomConstraint 属性中按 SQL 语法指定约束规则; 并用字段的 ConstraintErrorMessage 属性指定违反规则后的错误提示. procedure TForm1.FormCreate(Sender: TObject); begin with TIntegerField.Create(Self) do begin FieldName := ''ID''; CustomConstraint := ''x>0 and x<100''; { 其中的 x 是随意的, 表示当前字段值 } ConstraintErrorMessage := ''Err1''; DataSet := ClientDataSet1; end; with TStringField.Create(Self) do begin FieldName := ''Name''; Size := 11; CustomConstraint := ''x Like ''''张%''''''; { 假如只要姓张的 } ConstraintErrorMessage := ''Err2''; DataSet := ClientDataSet1; end; with TStringField.Create(Self) do begin FieldName := ''Sex''; Size := 2; { 如果使用 TWideStringField 这里应该是 1 } CustomConstraint := ''x in(''''男'''', ''''女'''')''; { 只能输入: 男或女 } ConstraintErrorMessage := ''Err3''; DataSet := ClientDataSet1; end; with TStringField.Create(Self) do begin FieldName := ''Email''; Size := 21; CustomConstraint := ''Lower(x) Like ''''%@gmail.com''''''; { 假如只能是 Google 信箱} ConstraintErrorMessage := ''Err4''; DataSet := ClientDataSet1; end; ClientDataSet1.CreateDataSet; ClientDataSet1.AppendRecord([1, ''张三'', ''男'', ''123@gmail.com'']); ClientDataSet1.AppendRecord([2, ''张四'', ''女'', ''ABC@GMAIL.COM'']); end; { 禁用限制 } procedure TForm1.Button1Click(Sender: TObject); begin ClientDataSet1.DisableConstraints; end; { 启用限制 } procedure TForm1.Button2Click(Sender: TObject); begin ClientDataSet1.EnableConstraints; end; (责任编辑:admin)
|