You didn't say where it was all records in your grid or just the visible ones:
Any how heres is an example for doing both:
This example uses a form with a cxGrid, cxButton and a cxMemo. Plus a dxMemdataset
Here is the DFM code:
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
First thing first at form create I generate some random Data:
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;
Since my grid is bound to the dataset data will show up on screen.
Here is my form definition:
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;
Then you just have to push the button:
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;
So there you see Filtered records are the one you have in your grid after possibly after a filter had been applyed. Visible Record are the onec actually visible on the screen.