Hi all
I need to be output the results in a Tdbgrid to a tstringgrid so I can work with it in a spreadsheet.
I am just using a simple DBF file.
procedure tmainform.CopyDBGridToStringGrid(DBGrid: TDBGrid; StringGrid: TStringGrid);
var
i, j: Integer;
dbb:tdataset;
begin
dbb:=dbgrid.DataSource.DataSet;
StringGrid.RowCount := DBGrid.DataSource.DataSet.RecordCount + 1;
StringGrid.ColCount := DBGrid.Columns.Count;
// Set the header
for i := 0 to DBGrid.Columns.Count - 1 do
begin
StringGrid.Cells[i, 0] := DBGrid.Columns[i].Title.Caption;
end;
// Copy data
DBGrid.DataSource.DataSet.First;
for i := 1 to StringGrid.RowCount - 1 do
begin
for j := 0 to dbb.FieldCount - 1 do
begin
StringGrid.Cells[j, i] := DBGrid.Columns[j].Field.AsString;
end;
DBGrid.DataSource.DataSet.Next;
end;
end;
This works on filtered and not filtered data, but i am a bit lost here.
It copies the displayed data in the DBgridview perfectly, but it repeats the last Row of data about two hundred times.
It just has to be one of these loops causing it, but I am too blind to see where this went wrong.
Thank you for all the help.
-Peter