CSDN博客

img WuChenCan

给数据添加计算字段

发表于2004/7/9 16:21:00  717人阅读

{>------------------------------------------------------------
 过程功  能: 给数据添加计算字段
 修改改说明:
   增加参数: name : string
             Xtype: integer 用以指定字段类型
                    0: 为Integer 1:为String
       作用: 用以指定要添加计算字段的字段名称,有利于扩展过程的作用域
<-------------------------------------------------------------}
procedure AddCalcField(Cds: TClientDataset; aName: string; Xtype: integer);
Var
  FieldList:TList;
  FieldItem:TFieldInfo;
  FData: OleVariant;
  I:integer;
begin
  If Not Cds.Active Then
     Exit;
  FData := Cds.data;
  FieldList:=TList.Create;
  For I:=0 to Cds.FieldDefs.Count-1 Do
  Begin
    New(FieldItem);
    FieldItem^.FieldClass:=Cds.FieldDefs.Items[i].FieldClass;
    FieldItem^.FieldName :=Cds.FieldDefs.Items[i].name;
    FieldItem^.size := Cds.FieldDefs.Items[i].size;
    FieldItem^.Index := Cds.FieldDefs.Items[i].Index;
    FieldList.Add(FieldItem);
  End;
  if Cds.FindField(aName)<>nil then Exit;  //如果已创建过不再创建'GridXh'
 
  Cds.Close;
  Cds.FieldDefs.BeginUpdate ;
  {>-----------创建计算字段----------------}
  Case Xtype of
  0:
    With TIntegerField.Create(Cds) Do
    Begin
      FieldName :=aName;
      DisplayLabel :=aName;
      FieldKind :=fkCalculated;
      Visible:=True;
      Dataset:=Cds;
    End;
  1:
    With TStringField.Create(Cds) Do
    Begin
      FieldName :=aName;
      DisplayLabel :=aName;
      FieldKind :=fkCalculated;
      Visible:=True;
      Dataset:=Cds;
    End;
  end;//Case
  {<-------------------------------------}
  Cds.FieldDefs.EndUpdate;
  For I:=0 to FieldList.Count-1 Do
  Begin
    if Cds.FindField(TFieldInfo(FieldList.Items[i])^.FieldName)=nil then
      with TFieldInfo(FieldList.Items[i])^.FieldClass.Create(Cds) do
      begin
        FieldName := TFieldInfo(FieldList.Items[i])^.FieldName;
        size := TFieldInfo(FieldList.Items[i])^.size;
        Index := TFieldInfo(FieldList.Items[i])^.Index;
        Dataset := Cds;
        Freemem(FieldList.Items[I],Sizeof(TFieldInfo));
      end;
  End;
  FieldList.Free ;
  Cds.AppendData(Fdata, false);
0 0

相关博文

我的热门文章

img
取 消
img