簡體   English   中英

如何遍歷cxgrid上的記錄-Delphi XE2

[英]How to loop through records on a cxgrid - Delphi xe2

您如何遍歷cxgrids記錄? 即使delphi程序從上到下遍歷/檢查cxgrid中的每個記錄。

我有一個cxgrid,它顯示來自tadquery的記錄,而該tadquery正在與數據庫表進行通信,如果這有幫助的話。

為TcxGrid中的TcxGridDBTableView執行此操作的示例代碼,並且還對下面的DataSet進行了迭代。 無論是否過濾了DataSet,這兩個示例都將起作用。

TcxGrid的示例假定您已從面板中拉出網格,向其中添加了TcxDBTableView,向其添加了數據集的列,並且將對象檢查器中的所有網格屬性保留為默認值,但TableView的KeyFieldNames除外。設置為數據集的主鍵,在本例中為“ 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;

what you asked, but if you want to iterate a dataset without doing it using a TcxGrid, it's actually much simpler: 順便說一句,我知道這您要的,但是如果您要迭代數據集而不使用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;

您沒有說是網格中的所有記錄還是可見的記錄:

無論如何,這都是同時做這兩個例子:

本示例使用帶有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;

因此,您可能會看到過濾的記錄是您可能在應用過濾器之后在網格中擁有的記錄 可見記錄是屏幕上實際可見的記錄

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM