RAD Studio VCL Reference
|
Implements a virtual method to position the dataset on the record to which a specified bookmark points.
GotoBookmark calls an internal method that is an empty stub in TDataSet. Descendants of TDataSet redeclare and implement the internal method so that GotoBookmark makes the record identified by the Bookmark parameter active.
C++ Examples:
/* This example uses a button to copy the value of a field in the previous record into the corresponding field in the current record. */ /* To ensure that the button is disabled when there is no previous record, the OnDataChange event of the DataSource detects when the user moves to the beginning of file (BOF property becomes true), and disables the button. Detection occurs on scrolling and editing, not selection with the mouse. */ void __fastcall TForm1::DS2DataChange(TObject *Sender, TField *Field) { if (Customers->Bof) Button2->Enabled = False; else Button2->Enabled = True; } void __fastcall TForm1::Button2Click(TObject *Sender) { TBookmark SavePlace; Variant PrevValue; // get a bookmark so that we can return to the same record SavePlace = Customers->GetBookmark(); Customers->FindPrior();// move to prior record PrevValue = Customers->FindField("Field1")->Value; // get the value // Move back to the bookmark // this may not be the next record anymore // if something else is changing the dataset asynchronously Customers->GotoBookmark(SavePlace); Customers->Edit(); Customers->FindField("Field1")->Value = PrevValue; // set the value Customers->FreeBookmark(SavePlace); }
Delphi Examples:
{ This example uses a button to copy the value of a field in the previous record into the corresponding field in the current record. } procedure TForm1.Button1Click(Sender: TObject); var SavePlace: TBookmark; PrevValue: Variant; begin with Customers do begin { get a bookmark so that we can return to the same record } SavePlace := GetBookmark; try { move to prior record} FindPrior; { get the value } PrevValue := FindField('Field2').Value; {Move back to the bookmark this may not be the next record anymore if something else is changing the dataset asynchronously } GotoBookmark(SavePlace); { Set the value } Edit; FindField('Field2').Value := PrevValue; { Free the bookmark } finally FreeBookmark(SavePlace); end; end; end; { To ensure that the button is disabled when there is no previous record, the OnDataChange event of the DataSource detects when the user moves to the beginning of file (BOF property becomes true), and disables the button. Detection occurs on scrolling and editing, not selection with the mouse. } procedure TForm1.DS2DataChange(Sender: TObject; Field: TField); begin if Customers.Bof then Button1.Enabled := False else Button1.Enabled := True; end;
{ The following example copies the selected rows in a db grid to a list box. Set the db grid Options dgRowSelect, dgAlwaysShowSelection and dgMultiSelect to True. Make a multiple selecton using the CNTL key. This example requires a TDataSet associated with a TDataSource and a TDBGrid.} procedure TForm1.Button2Click(Sender: TObject); var i, j: Integer; s: string; begin if DBGrid2.SelectedRows.Count>0 then with DBGrid2.DataSource.DataSet do for i:=0 to DBGrid2.SelectedRows.Count-1 do begin GotoBookmark(DBGrid2.SelectedRows.Items[i]); for j := 0 to FieldCount-1 do begin if (j>0) then s:=s+', '; s := s + FindField(Fields.Fields[j].FieldName).AsString; end; Listbox1.Items.Add(s); s:= ''; end; end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|