Reputation: 11
In Delphi, I have a component:
dbgridsimpipa: Tdbgrid,
DSpipa: Tdatasource,
Qpipa: Tquery,
btnViewdata: tbutton
btnRecNumber: tbutton.
I search the table with this code:
procedure TfrmMain.btnViewDataClick(Sender: TObject);
var
st,st4,y0,y1,y2: string;
begin
st:='5';
grid2.Enabled := True;
st5 := 'SELECT tblRekapROT.CY2IDR, tblRekapROT.Produk, tblRekapROT.Total, tblRekapROT.ARRWL, tblRekapROT.ARBYL, tblRekapROT.Status '+
'FROM tblRekapROT '+
'WHERE ((tblRekapROT.Status Like ''%'+st+'%''))';
Qpipa.Close;
Qpipa.SQL.Clear;
Qpipa.SQL.Add(st5);
Qpipa.Open;
label22.Caption:= dbgridsimpipa.Fields[0].AsString;
end;
Then I count the records in DBGrid
with :
procedure TfrmMain.btnRecNumberClick(Sender: TObject);
var
vCount: Integer;
vBookmark: TBookmark;
begin
vCount := 0;
with DBGridSimPipa.DataSource.DataSet do
begin
vBookmark := GetBookmark;
First; // move to the first record
while not EoF do
begin
vCount := vCount + 1;
Next; // move to next record
end;
GotoBookmark(vBookmark);
FreeBookmark(vBookmark);
end;
ShowMessage('We found ' + IntToStr(vCount) + ' records');
end;
How do I get the value of rows in DBGrid
in one column? Maybe like this:
//label1.Caption:= dbgridsimpipa.DataSource.DataSet.First //record vtotal row 1
//label2.Caption:= dbgridsimpipa.DataSource.DataSet.second //record vtotal row 2
//label3.Caption:= dbgridsimpipa.DataSource.DataSet.third //record vtotal row 3
This a screenshot of my program.
Upvotes: 0
Views: 2266
Reputation: 6402
First of all welcome to StackOverflow.
You have some misunderstanding about how dataset works. First of all if you want to know how many rows you found you can just call the RecordCound property:
procedure TfrmMain.btnRecNumberClick(Sender: TObject);
begin
ShowMessage('We found ' + IntToStr(DBGridSimPipa.DataSource.DataSet.RecordCount) + ' records');
end;
If you want to Access the first row in a Dataset you can call First
but there are no Second
or Third
property so there you'll have to ude the property RecordNo
DataSet.First
is the same as Dataset.RecordNo := 0
The second row you get by calling Dataset.RecordNo := 1
etc.
THen you wanted to put then content of a row into a label. There are no build in way of doing that, so you'll have to write you own function to to this.
function DataRowToString(const aDataset: TDataSet; aRecNo: Integer): String;
var
i: Integer;
begin
aDataset.RecNo := aRecNo;
Result := '';
for i := 0 to aDataset.FieldCount - 1 do
Result := Result + aDataset.Fields[i].AsString + ' ';
Result := Trim(Result);
end;
Finally we just need to call the newly created function:
label1.Caption:= DataRowToString(dbgridsimpipa.DataSource.DataSet, 0); //record vtotal row 1
label2.Caption:= DataRowToString(dbgridsimpipa.DataSource.DataSet, 1); //record vtotal row 2
label3.Caption:= DataRowToString(dbgridsimpipa.DataSource.DataSet, 2);//record vtotal row 3
Upvotes: 2