ClientDataSet动态创建lookup字段?

解决方案 »

  1.   

    var
       TTotal:array[0..7]of TStringField;//建立TStringField
       tempfieldnames:tstringlist;
       T: TStringField;//建立lookup型field
      tempsql,tempsql_1:string;
      i:integer;
    begin
       //从数据库中取出数据
      tempsql:='SELECT * FROM et_activity WHERE  companyid = '+quotedstr(dbmoduel.ClientDataSet1.fields[0].AsString) +'and contactid='+quotedstr(dbmoduel.ClientDataSet2.fields[0].AsString);
      dbmoduel.ClientDataSet3.close;
      dbmoduel.ClientDataSet3.CommandText:=tempsql;
      dbmoduel.ClientDataSet3.open;
      dbmoduel.ClientDataSet3.close;
      //对应于lookupdataset
      tempsql_1:='select * from activity_code';
      dbmoduel.ClientDataSettemp1.Close;
      dbmoduel.ClientDataSettemp1.CommandText:=tempsql_1;
      dbmoduel.ClientDataSettemp1.Open;
     ///// 
     tempfieldnames:=tstringlist.Create;
      tempfieldnames.CommaText:='companyid,contactid,activitynum,activitycode,sourcecode,userid,activity_status,sender';
    ///////////对应于数据库的字段建立8个Tfield
      for i:=0 to 7 do
      begin
      TTotal[i] := TStringField.Create(dbmoduel.ClientDataSet3);
      TTotal[i] .FieldName := tempfieldnames[i];
      TTotal[i] .DataSet :=dbmoduel.ClientDataSet3;
      dbmoduel.ClientDataSet3.FieldDefs.UpDate;
      end;
    /////////////建立lookup型的filed
      T := TStringField.Create(dbmoduel.ClientDataSet3);
      T.FieldName := 'ac';
      T.DisplayLabel:='活动代码';
      T.DataSet := dbmoduel.ClientDataSet3;
      T.FieldKind := fkLookup;
      T.Size := 200;
      T.LookupDataSet := dbmoduel.ClientDataSettemp1;
      T.LookupKeyFields := 'codeid';
      T.KeyFields := 'activitycode';
      t.ReadOnly:=false;
      t.LookupCache:=true;
      T.Lookup:=true;
      T.LookupResultField := 'description';
      dbmoduel.ClientDataSet3.FieldDefs.UpDate;
      /////////////////////
      dbmoduel.ClientDataSet3.open;
      

  2.   

    var
      fldLookup:TField;
    begin
      fldLookup := TField.Create(self);
      fldLookup.ReadOnly:=false;
      fldLookup.LookupCache:=true;
      fldLookup.Size := 1000;
      fldLookup.Lookup:=true;
      fldLookup.FieldName := 'ID';
      fldLookup.DisplayLabel:='名称';
      fldLookup.DataSet := Dataset;
      fldLookup.FieldKind := fkLookup;
      fldLookup.LookupDataSet := Dataset1;
      fldLookup.LookupKeyFields := 'ID';
      fldLookup.KeyFields := 'Code';
      fldLookup.LookupResultField := 'CodeName';
    end;
      

  3.   

    procedure TForm1.Button3Click(Sender: TObject);
    var
    T : TStringField;
    begin
    cds1.close;
    T := TStringField.Create(cds1);
    cds1.Fields.Add(T);
    with T do
    begin
    dataset:=cds1;
    FieldKind := fkLookup;
    FieldName := 'ac';
    DisplayLabel:='活动代码';
    size:=200;
    //Name:=name+'AggregateField'+FieldName;
    LookupDataSet := cds2;
    LookupKeyFields :='codeid';
    LookupResultField :='description';
    KeyFields :='activitycode';
    readonly:=false;
    Lookup := True;
    LookupCache:=true;
    //cds1.FieldDefs.Add(Name,fTString,20, False);
    cds1.FieldDefs.Update;
    end;   
    cds1.open;
    end;多谢两位大侠,我参照2位的写的如上代码:
    如果cds1为close的话,程序出错,提示cds1:field name missed
    如果为open,则提示 cds1:can not perform this operation on an open dataset
    各位大侠,指点一下这是为什么啊?