[英]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.