比较DBgrid和ClientDataSet字段名称

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了比较DBgrid和ClientDataSet字段名称相关的知识,希望对你有一定的参考价值。

如何比较clientdataset和dbgrid的字段名?例如,如果在dgbrid中存在,则检查clientdataset中可见的每个字段?

答案

解:

for i := 0 to cds.Fields.Count-1 do begin
   for j := 0 to grid.Columns.Count-1 do begin
       if cds.Fields[i].FieldName = grid.Columns[j].FieldName then begin
          ShowMessage('Field: ' + cds.Fields[i].FieldName);
          Break;
       end;
   end;
end;

工作范例:

unit frmMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, DBClient, Grids, DBGrids;

type
  TForm1 = class(TForm)
    btnCheck: TButton;
    ds: TDataSource;
    cds: TClientDataSet;
    grid: TDBGrid;
    procedure btnCheckClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnCheckClick(Sender: TObject);
var
   i, j: Integer;
begin
   // Controls
   if cds.Active then cds.Close;
   ds.DataSet := cds;
   grid.DataSource := ds;

   // Dataset. Create dataset with three fields.
   cds.FieldDefs.Clear;
   cds.FieldDefs.Add('FIELD1', ftString, 10, False);
   cds.FieldDefs.Add('FIELD2', ftString, 20, False);
   cds.FieldDefs.Add('FIELD3', ftString, 30, False);
   try
      cds.CreateDataSet;
      cds.Open;
   except
      on E: Exception do begin
         ShowMessage('Error: ' + E.Message);
         Exit;
      end{on};
   end{try};

   // Grid. Set grid with one column.
   grid.Columns.Clear;
   with grid.Columns.Add do begin
      FieldName := 'FIELD1';
   end{with};

   // Check every field
   for i := 0 to cds.Fields.Count-1 do begin
      for j := 0 to grid.Columns.Count-1 do begin
          if cds.Fields[i].FieldName = grid.Columns[j].FieldName then begin
             ShowMessage('Field: ' + cds.Fields[i].FieldName);
             Break;
          end{if};
      end{if};
   end{if};
end;

end.

笔记:

您可以检查Field.Visible和/或Column.Visible属性。

以上是关于比较DBgrid和ClientDataSet字段名称的主要内容,如果未能解决你的问题,请参考以下文章

delphi的DBgrid怎么获取当前记录的上一条bookmark,dbgrid的数据源是dataset

53分(我的所有)请教:关于ClientDataSet新增记录问题(请富翁们关注!谢谢)

delphi问题,我想指定选中dbgrid的第X行

delphi dbgrid

Delphi中统一显示表格字段名的高效方法

ClientDataSet建立索引和排序