Page 1 of 1

Error copying data in a DBGrid control to another using Zeos

Posted: 03.02.2015, 03:10
by elidorio2
Hello all,

I'm using the code below, to copy data from a dbgrid control to the other, this showing this error: bookmark was not found
I'm using Lazarus 1.2.6 with FPC 2.6.4 and Zeos Components windows 7 32.

What pose be wrong?

The code I am using is this:
procedure Tfrmprodgrupo.btnadicionaClick(Sender: TObject);
var
Idx: integer;
begin
try
if dbgprod.SelectedRows.Count > 0 then
begin
for Idx := 0 to dbgprod.SelectedRows.Count - 1 do
begin
// posiciona o dataset de origem no "n" registro selecionado
qryprod.GotoBookmark(pointer(dbgprod.SelectedRows.Items[Idx]));

// move os dados do dataset de origem para destino aqui
qrygrupo.Append;
qrygrupocd_fil.AsInteger := dmcad.filial;
qrygrupocd_grupo_trib.AsInteger := StrToInt(dbedtgrupo.Text);
qrygrupocd_prod.Value := qryprodcd_prod.Value;
qrygruponm_usuinc.AsString := frmmenu.usuariolog;
qrygrupodt_usuinc.AsDateTime := (Date + Time);
qrygrupo.Post;
qryprod.Refresh;
qrygrupo.Refresh;
qrycontprod.Close;
qrycontprod.Open;
qrycont2.Close;
qrycont2.ParamByName('cd_fil').AsInteger := dmcad.filial;
qrycont2.ParamByName('cd_grupo').AsInteger := StrToInt(dbedtgrupo.Text);
qrycont2.Open;
end;
end;
except
on E: Exception do
begin
raise Exception.Create('Ocorreu um erro:' + #13 + E.message);
end;
end;

Re: Error copying data in a DBGrid control to another using Zeos

Posted: 03.02.2015, 12:10
by marsupilami
Hello elidorio2,

I assume that the error happens at this line:
qryprod.GotoBookmark(pointer(dbgprod.SelectedRows.Items[Idx]));

I am not 100% sure but by using GotoBookmark you probably change the contents of SelectedRows. Maybe it is a better idea to first save the bookamrks to an array or something like that and then use that array for selecting the records you need to copy.
Best regards,

Jan

Re: Error copying data in a DBGrid control to another using Zeos

Posted: 04.02.2015, 20:26
by markus
Hi,
Maybe it is a better idea to first save the bookamrks to an array or something
Grid.SelectedRows is already type TBookmarkList.

I suggest using another variable of type TBookmark:

Code: Select all

TBookmark selRow = dbgprod.SelectedRows.Items[Idx];
qryprod.GotoBookmark(selRow);
btw:
you should also think about using Query.DisableControls before whole operation
and Query.EnableControls when it's finished - otherwise you'll see scrolling through query when going to selected rows.
Also save your current row to separate TBookmark, and go there after whole operation.


Best regards,
Marek