0

你如何循环遍历 cxgrids 记录?即让delphi程序从上到下遍历/检查cxgrid中的每条记录。

我有一个 cxgrid,它显示来自 tadquery 的记录,如果这有帮助的话,它正在与数据库表对话。

4

2 回答 2

2

示例代码为 TcxGrid 中的 TcxGridDBTableView 执行此操作,并在下面迭代 DataSet。无论 DataSet 是否被过滤,这两个示例都将起作用。

TcxGrid 的示例假定您已从调色板中拉出网格,向其中添加了 TcxDBTableView,向其中添加了数据集的列,并将对象检查器中的所有网格属性保留为默认值,除了 TableView 的 KeyFieldNames,它需要设置为 DataSet 的主键,在我的例子中是“FilesID”。这是为了能够识别 TableView 中给定行的数据集记录 - 您可以获得该行的键值 ID,如下所示:

      ID := cxGrid1DBTableView1.DataController.GetRecordId(TopRowIndex + Row);

然后调用 CDS1.Locate() 使用 ID 值来检索记录。

TBookmark 用于在操作之前记录当前的 CDS 记录,并在之后返回到它。对 DisableControls 和 EnableControls 的调用是为了防止在操作进行时更改 cxGrid(以及连接到 CDS 的任何其他 DB 感知控件)。

procedure TForm1.IterateVisibleGridRows;
var
  BM : TBookmark;
  TopRowIndex : Integer;
  VisibleCount : Integer;
  Row : Integer;
  ID : Integer;
  Controller : TcxGridTableController;
  ViewInfo : TcxGridTableViewInfo;
begin
  BM := CDS1.GetBookmark;
  try

    Controller := cxGrid1DBTableView1.Controller;
    ViewInfo := TcxGridTableViewInfo(Controller.ViewInfo);

    TopRowIndex := Controller.TopRowIndex;
    VisibleCount := ViewInfo.RecordsViewInfo.VisibleCount;

    CDS1.DisableControls;

    Row := 0;
    while Row < VisibleCount do begin
      ID := cxGrid1DBTableView1.DataController.GetRecordId(TopRowIndex + Row);
      if CDS1.Locate('FilesID', ID, []) then begin
        // Do what you want here
      end;
      Inc(Row);
    end;

  finally
    CDS1.GotoBookmark(BM);
    CDS1.FreeBookmark(BM);
    CDS1.EnableControls;
  end;
end;

顺便说一句,我知道这不是你问的,但是如果你想迭代一个数据集而不使用 TcxGrid,它实际上要简单得多:

procedure IterateDataSetRows(DataSet : TDataSet);
var
  BM : TBookmark;
begin
  BM := CDS1.GetBookmark;
  try
    // delete the following 2 lines and the one in the finally block if you don't want a "Wait" cursor
    Screen.Cursor := crSqlWait;
    Screen.ActiveForm.Update;

    DataSet.DisableControls;
    DataSet.First;

    while not DataSet.Eof do begin
        // Do what you want here
      DataSet.Next;
    end;

  finally
    DataSet.GotoBookmark(BM);
    DataSet.FreeBookmark(BM);
    DataSet.EnableControls;
    Screen.Cursor := crDefault;
  end;
end;
于 2014-12-10T15:48:33.553 回答
1

您没有说网格中的所有记录或可见记录在哪里:

任何方式都是这样做的一个例子:

此示例使用带有 cxGrid、cxButton 和 cxMemo 的表单。加上一个 dxMemdataset

这是 DFM 代码:

object Form20: TForm20
  Left = 0
  Top = 0
  Caption = 'Form20'
  ClientHeight = 299
  ClientWidth = 462
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  DesignSize = (
    462
    299)
  PixelsPerInch = 96
  TextHeight = 13
  object cxGrid1: TcxGrid
    Left = 0
    Top = 0
    Width = 299
    Height = 299
    Align = alLeft
    TabOrder = 0
    ExplicitHeight = 635
    object cxGrid1DBTableView1: TcxGridDBTableView
      Navigator.Buttons.CustomButtons = <>
      DataController.DataSource = DataSource1
      DataController.Summary.DefaultGroupSummaryItems = <>
      DataController.Summary.FooterSummaryItems = <>
      DataController.Summary.SummaryGroups = <>
      object cxGrid1DBTableView1RecId: TcxGridDBColumn
        DataBinding.FieldName = 'RecId'
        Visible = False
      end
      object cxGrid1DBTableView1Field1: TcxGridDBColumn
        DataBinding.FieldName = 'Field1'
      end
      object cxGrid1DBTableView1Field2: TcxGridDBColumn
        DataBinding.FieldName = 'Field2'
      end
    end
    object cxGrid1Level1: TcxGridLevel
      GridView = cxGrid1DBTableView1
    end
  end
  object cxButton1: TcxButton
    Left = 305
    Top = 8
    Width = 154
    Height = 25
    Caption = 'Do the trick'
    TabOrder = 1
    OnClick = cxButton1Click
  end
  object cxMemo1: TcxMemo
    Left = 305
    Top = 39
    Anchors = [akLeft, akTop, akRight, akBottom]
    Lines.Strings = (
      'cxMemo1')
    TabOrder = 2
    Height = 260
    Width = 154
  end
  object dxMemData1: TdxMemData
    Indexes = <>
    SortOptions = []
    Left = 160
    Top = 144
    object dxMemData1Field1: TIntegerField
      FieldName = 'Field1'
    end
    object dxMemData1Field2: TIntegerField
      FieldName = 'Field2'
    end
  end
  object DataSource1: TDataSource
    DataSet = dxMemData1
    Left = 168
    Top = 152
  end
end

首先在表单创建我生成一些随机数据:

procedure TForm20.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  randomize;
  dxMemData1.DisableControls;
  try
    dxMemData1.Open;
    for i := 0 to 999 do
      dxMemData1.AppendRecord([i, Random(500), Random(500)]);
  finally
    dxMemData1.EnableControls;
  end;
end;

由于我的网格绑定到数据集数据将显示在屏幕上。

这是我的表单定义:

type
  TForm20 = class(TForm)
    dxMemData1: TdxMemData;
    dxMemData1Field1: TIntegerField;
    dxMemData1Field2: TIntegerField;
    cxGrid1DBTableView1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    DataSource1: TDataSource;
    cxGrid1DBTableView1RecId: TcxGridDBColumn;
    cxGrid1DBTableView1Field1: TcxGridDBColumn;
    cxGrid1DBTableView1Field2: TcxGridDBColumn;
    cxButton1: TcxButton;
    cxMemo1: TcxMemo;
    procedure FormCreate(Sender: TObject);
    procedure cxButton1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

然后你只需要按下按钮:

procedure TForm20.cxButton1Click(Sender: TObject);
var
  RecNo, i: Integer;
  cxCustomGridRecordViewInfo: TcxCustomGridRecordViewInfo;
  s: string;
begin
  s := Format(
    'You have' + sLineBreak +
    '  %d records in your Dataset' + sLineBreak +
    '  %d records in your grid' + sLineBreak +
    '  %d VISIBLE records in your grid'
    , [
    cxGrid1DBTableView1.DataController.RecordCount,
      cxGrid1DBTableView1.DataController.FilteredRecordCount,
      cxGrid1DBTableView1.ViewInfo.VisibleRecordCount
      ]
      );

  MessageDlg(s, mtInformation, [mbOK], 0);

  cxMemo1.Lines.BeginUpdate;
  cxMemo1.Lines.Clear;

  cxMemo1.Lines.Add(' *** Filtered Records ***');

  for i := 0 to cxGrid1DBTableView1.DataController.FilteredRecordCount - 1 do
  begin
    RecNo := cxGrid1DBTableView1.DataController.FilteredRecordIndex[i];
    cxMemo1.Lines.Add(cxGrid1DBTableView1.DataController.Values[RecNo, 1]);
  end;

  cxMemo1.Lines.Add(' *** Visible Records ***');

  for i := 0 to cxGrid1DBTableView1.ViewInfo.VisibleRecordCount - 1 do
  begin
    cxCustomGridRecordViewInfo := cxGrid1DBTableView1.ViewInfo.RecordsViewInfo[i];
    cxMemo1.Lines.Add(cxCustomGridRecordViewInfo.GridRecord.Values[1]);
  end;

  cxMemo1.Lines.EndUpdate;
end;

因此,您在此处看到过滤记录是您在应用过滤器之后可能在网格中拥有的记录。Visible Record是屏幕上实际可见的 onec。

于 2014-12-11T11:00:29.527 回答