TClientDataSet[22]: 数组字段与 ObjectView
生活随笔
收集整理的這篇文章主要介紹了
TClientDataSet[22]: 数组字段与 ObjectView
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
數組字段(ftArray) 與 ADT 字段類似(設計過程一樣), 定義數組字段時只需定義一個元素來說明元素類型.
本例同時測試 ObjectView 為 True 和 False 時的不同效果.
下面是在運行時實現的數組字段:
//先在窗體上放置: DBGrid1、DataSource1、ClientDataSet1 并關聯, 然后:procedure TForm1.FormCreate(Sender: TObject); begin // ClientDataSet1.ObjectView := True;{ 先定義兩個基本字段 }with ClientDataSet1.FieldDefs.AddFieldDef do beginName := 'ID';DataType := ftInteger;end;with ClientDataSet1.FieldDefs.AddFieldDef do beginName := 'Group';DataType := ftString;Size := 7;end;{ 定義數組字段: 假如數組包括 4 個元素 }with ClientDataSet1.FieldDefs.AddFieldDef do beginName := 'Member';DataType := ftArray;Size := 4;ChildDefs.Add('TempName', ftString, 11); { 只需定義一個子元素來說明元素類型 }end;ClientDataSet1.CreateDataSet;{ 添加記錄 }ClientDataSet1.AppendRecord([1, '一組', VarArrayOf(['A1','A2','A3','A4'])]);{ 下面的添加方式也可用于訪問 }ClientDataSet1.Append;ClientDataSet1['ID'] := 2;ClientDataSet1['Group'] := '二組';TArrayField(ClientDataSet1.FieldByName('Member'))[0] := 'B1';TArrayField(ClientDataSet1.FieldByName('Member'))[1] := 'B2';TArrayField(ClientDataSet1.FieldByName('Member')).FieldValues[2] := 'B3';TArrayField(ClientDataSet1.FieldByName('Member')).FieldValues[3] := 'B4';ClientDataSet1.Post;ClientDataSet1.Append;ClientDataSet1['ID'] := 3;ClientDataSet1['Group'] := '三組';TArrayField(ClientDataSet1.FieldByName('Member')).Fields[0].AsString := 'B1';TArrayField(ClientDataSet1.FieldByName('Member')).Fields[1].AsString := 'B2';TArrayField(ClientDataSet1.FieldByName('Member')).Fields[2].Value := 'B3';TArrayField(ClientDataSet1.FieldByName('Member')).Fields[3].Value := 'B4';ClientDataSet1.Post; end;{ 可以通過下面代碼查看 ObjectView 為 True 和 False 時的不同 } procedure TForm1.Button1Click(Sender: TObject); beginClientDataSet1.Close;ClientDataSet1.ObjectView := not ClientDataSet1.ObjectView;Text := Format('ObjectView: %s', [BoolToStr(ClientDataSet1.ObjectView, True)]);ClientDataSet1.Open; end;
總結
以上是生活随笔為你收集整理的TClientDataSet[22]: 数组字段与 ObjectView的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TI Davinci DM6446开发攻
- 下一篇: asp.net在线发送邮件,以前没做过,