查看表中ClientDataSet中包含的数据

时间:2017-05-18 14:28:05

标签: delphi

我需要你的帮助。我有一个ClientDataSet和一个TDBGrid表。我第一次建造一张桌子。什么?我仍然在这里想念数据。

   Procedure TForm1.AddLanguageGrid();
 var  lWord: Tword;
 lColumn: TColumn
 begin

  self.ClientDataSet1 := TClientDataSet.Create(self);
  self.ClientDataSet1.FieldDefs.Add('Brand_trousers', ftstring, 100);
  self.ClientDataSet1.FieldDefs.Add('Color', ftstring, 100);

  self.ClientDataSet1.CreateDataSet;
  self.ClientDataSet1.IndexFieldNames := 'id';
  self.ClientDataSet1.DisableControls;
  self.DataSource1.DataSet := self.ClientDataSet1;
  self.DBGrid1.DataSource := self.DataSource1;
  self.DBGrid1.Columns.Clear;    

  lColumn := TColumn.Create(self.DBGrid1.Columns);
  lColumn.Width := 40;
  lColumn.Title.Caption := 'Brand trousers';
  lColumn.FieldName := 'Brand_trousers';

  lColumn := TColumn.Create(self.DBGrid1.Columns);
  lColumn.Width := 150;
  lColumn.Title.Caption := 'Color';
  lColumn.FieldName := 'Color';

self.ClientDataSet1.Append;
self.ClientDataSet1.EnableControls;
end.

1 个答案:

答案 0 :(得分:3)

以下对我来说很好:

procedure TForm1.AddLanguage;
var
  //lWord: TWord;  TWord is not a standard type!!!
  lColumn: TColumn;  //  semicolon missing
begin

  ClientDataSet1 := TClientDataSet.Create(self);
  //  You need an ID Field if you want to set IndexFieldNames to it !!!
  ClientDataSet1.FieldDefs.Add('ID', ftInteger);
  ClientDataSet1.FieldDefs.Add('Brand_trousers', ftstring, 100);
  ClientDataSet1.FieldDefs.Add('Color', ftstring, 100);

  ClientDataSet1.CreateDataSet;
  ClientDataSet1.IndexFieldNames := 'ID';

  //ClientDataSet1.DisableControls;  <- Pointless !!!
  DataSource1.DataSet := ClientDataSet1;
  DBGrid1.DataSource := DataSource1;
  DBGrid1.Columns.Clear;

  lColumn := TColumn.Create(DBGrid1.Columns);
  lColumn.Width := 40;
  lColumn.Title.Caption := 'ID';
  lColumn.FieldName := 'ID';

  lColumn := TColumn.Create(DBGrid1.Columns);
  lColumn.Width := 40;
  lColumn.Title.Caption := 'Brand trousers';
  lColumn.FieldName := 'Brand_trousers';

  lColumn := TColumn.Create(DBGrid1.Columns);
  lColumn.Width := 150;
  lColumn.Title.Caption := 'Color';
  lColumn.FieldName := 'Color';

  //ClientDataSet1.Append;  <- Pointless when you can add a row and set its
  //  Field values in a single InsertRecord call !!!
  //  Also, Append leaves all the record's field values set to NULL until
  //  you assign other values which is probably why you could see nothing
  //  in the grid
  ClientDataSet1.InsertRecord([1, 'Some brand', 'Brown']);
  //ClientDataSet1.EnableControls; <- Pointless
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  AddLanguage;
end;

<强>更新

  

如果这不麻烦,请告诉我如何在表格中选择一行后将其内容输入TEdit。例如:Edit1.text&#34; Brand trousers&#34;,Edt2.text&#34; Colors

如果您使用TDBEdits而不是TEdits,这是最简单的,因为它们可以自动识别数据。

  • 在表单上放置其中的3个(来自Component Palette的Data Controls选项卡)。

  • 将每个DataSource属性设置为DataSource1

  • DataField属性设置为IDBrand_TrousersColor

您需要这样做才能显示当前CDS行的数据。但是,如果您还在表单中添加TDBNavigator并将其连接到DataSource1,则会更加轻松,因为它包含SaveCancel按钮,用于发布或取消对记录数据的更改