随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253


运行时:



//先在窗体上放置: DBGrid1、DataSource1、ClientDataSet1 并关联, 然后:

procedure TForm1.FormCreate(Sender: TObject);
begin
  //ClientDataSet1.ObjectView := True; { 这是默认的 }

  { 先定义两个基本字段 }
  with ClientDataSet1.FieldDefs.AddFieldDef do begin
    Name := 'ID';
    DataType := ftInteger;
  end;
  with ClientDataSet1.FieldDefs.AddFieldDef do begin
    Name := 'Name';
    DataType := ftString;
    Size := 7;
  end;

  { 定义 ftADT 字段, 联系方式(Contact)中包括: Tel、QQ、Email }
  with ClientDataSet1.FieldDefs.AddFieldDef do begin
    Name := 'Contact';
    DataType := ftADT;
    Size := 3;
    ChildDefs.Add('Tel', ftString, 12);
    ChildDefs.Add('QQ', ftString, 9);
    ChildDefs.Add('Email', ftString, 15);
  end;
  ClientDataSet1.CreateDataSet;

  { 添加记录, 这种最方便 }
  ClientDataSet1.AppendRecord([1, '张三', VarArrayOf(['9111111', '12222', 'aaa@bbb'])]);

  { 下面的其他添加方法也可用于访问 }
  ClientDataSet1.Append;
  ClientDataSet1['ID'] := 2;
  ClientDataSet1['Name'] := '李四';
  ClientDataSet1['Contact.Tel'] := '9222222';
  ClientDataSet1['Contact.QQ'] := '13333';
  ClientDataSet1['Contact.Email'] := 'ccc@ddd';
  ClientDataSet1.Post;

  ClientDataSet1.Append;
  ClientDataSet1.Fields[0].AsInteger := 3;
  ClientDataSet1.Fields[1].AsString := '王五';
  TADTField(ClientDataSet1.Fields[2])[0] := '9333333';
  TADTField(ClientDataSet1.Fields[2])[1] := '14444';
  TADTField(ClientDataSet1.Fields[2])[2] := 'eee@fff';
  ClientDataSet1.Post;

  ClientDataSet1.Append;
  ClientDataSet1.FieldByName('ID').Value := 4;
  ClientDataSet1.FieldByName('Name').Value := '孙六';
  TADTField(ClientDataSet1.FieldByName('Contact')).FieldValues[0] := '9444444';
  TADTField(ClientDataSet1.FieldByName('Contact')).FieldValues[1] := '15555';
  TADTField(ClientDataSet1.FieldByName('Contact')).FieldValues[2] := 'ggg@hhh';
  ClientDataSet1.Post;
end;


设计时:



这种复杂点的数据可以保存为 cds, 但保存不了 xml, 且 XMLData 也生成不了.

posted on 2010-02-05 10:14  万一  阅读(...)  评论(...编辑  收藏