Delphi using the calculated field

In many cases, the data we need and database -related data in other fields, such as the amount of orders for the product number and unit price. In the application, to display the amount of orders for specific items in the display at the same time, usually to create a field, to be multiplied in the field prior to displaying the amount of value stored in this field. However, if the user to modify the number or unit price, it would have to change the amount of value. The immediate problem was complicated, but fortunately Delphi provides an easy way to define additional fields in the database table. This field is called a field (Calculatedfields) calculated, they are database tables of other fields is based. Calculated field advantage is the calculated value is not stored in the database, but must be calculated each time the recording changes, and the need to access and display fields. Below the calculated field using a method described below.
  1. Create a new project file project1, place a Table on form1, a Datasource and a Dbgrid control, Datasource1 the Datasource property to Table1, Dbgrid1 the Datasource property to Datasource1. Table1 database table used for Order. DB , of the Table1 Active attribute is set to True. Here is the definition of Table1:

Field name
Types of
Description Field Meaning
Productid
+ Products
Numbering
Quantity
N
Quantity
Price
$
unit price

  2. Add the calculated field Cash. With a mouse right-click Table1 components in the pop-up menu, choose FieldsEditor ... entry into the field editor; then right-click the field editor field display area in the pop-up menu choose Add Fields ... items, the <fontface =? "bookantiqua"> Table1 all fields have joined the field editor; again display area right-click the field editor field in the pop-up menu, choose new  field, ... entry into the new field editor, set Name the new field is Cash, Type of <fontface = "bookantiqua"> Currency , FieldType of the calculated , the command button mouse click OK, and complete the addition of the calculated field Cash. 
  3. Add <fontface = "bookantiqua"> Table1 the OnCalcFields event. Its code is:

procedureTForm1.Table1CalcFields(DataSet: TDataSet);
var
quantity1:single;
price1:Currency;
begin
quantity1:=Table1.FieldByName(quantity).asfloat; //数量
price1:=Table1.FieldByName(price).asCurrency; //单价
Table1.FieldByName(cash).asCurrency:=quantity1*price1; //应付资金
end;

  Run the program, and you can move freely modified record in Quantity and Price column of data tables, Cash bar immediately after the show with the appropriate amount of value.

Guess you like

Origin www.cnblogs.com/jijm123/p/11700387.html