简体   繁体   中英

How to display a record field from dbgrid to an editbox

I have built an access database and connected it with an ADOquery and Datasource .I have built a table named BagCost which has field names bag size and cost . I have attached a DBgrid into a form and linked it with the "Bagcost" table. The Dbgrid has the following fields:

在此处输入图片说明

How can I display the four costs from the dbgrid column to different editboxes at runtime?

Explanations:

I hope I understand you correctly. What you need to do is to read data from each of your four records. Use First and Next methods to change the active record, Eof method to test if the active record is the last record in a dataset. Optionally (for large number of records), use DisableControls and EnableControls methods to prevent data-aware controls from updating every time the active record changes.

Example:

Next is a basic example, that uses AfterOpen event to read field values from each record of your query.

procedure TfrmMain.qryAfterOpen(DataSet: TDataSet);
begin
   DataSet.DisableControls;
   try
      DataSet.First;
      if not DataSet.Eof then Edit1.Text := DataSet.FieldByName('cost').AsString else Edit1.Text := 'Record not found';
      DataSet.Next;
      if not DataSet.Eof then Edit2.Text := DataSet.FieldByName('cost').AsString else Edit2.Text := 'Record not found';
      DataSet.Next;
      if not DataSet.Eof then Edit3.Text := DataSet.FieldByName('cost').AsString else Edit3.Text := 'Record not found';
      DataSet.Next;
      if not DataSet.Eof then Edit4.Text := DataSet.FieldByName('cost').AsString else Edit4.Text := 'Record not found';
      DataSet.First;
   finally
      DataSet.EnableControls;
   end{try};
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM