如何循环cxgrids记录?即make delphi程序从上到下遍历/检查cxgrid中的每条记录。
我有一个cxgrid,它显示来自tadquery的记录,如果这对于数据库表有帮助,那么无论如何都会这样做。
答案 0 :(得分:2)
为TcxGrid中的TcxGridDBTableView执行此操作的示例代码,以及迭代下面的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-aware控件)。
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;
答案 1 :(得分: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;
因此,您可以看到已过滤的记录是可能在应用过滤器之后您的网格中的内容。 可见记录是屏幕上实际可见的onec。