Field of a DataSet in Delphi


By default, Delphi automatically creates the TField components at run time, each time the program opens a dataset component. Each field can be used for reading or modifying the data of the current record, using its VALUE property or type-specific properties, like the following:


AsBoolean : Boolean

AsDateTime: TDateTime

AsFloat : Double

AsInteger : LongInt

AsString : string

AsVariant : Variant


example:


var

        strName: string;

begin

strName := Table1.Fields[0].AsString;

strName := Table1.FieldByName('LastName').AsString;

strName := Table1.FieldValues ['LastName'];

StrName := Table1 ['Lastname'];


The above statements have similar result or effect on the variable strName.


IF the field was created with the Field Editor at run-time, you can access it in the following manner as follows:


strName := Table1LastName.AsString;

strName := Table1LastName.Value;


when you set field properties related to data input or output, the change applies to every record in the table.


ex.:


procedure TForm2.Btn1Click

(Sender: TObject);

begin

    (Table1.FieldByName('Population') as TFloatField).DisplayFormat := '###,###.##';

end;


procedure TForm2.Btn2Click

(Sender: TObject);

begin

    ShowMessage(string (Table1 ['Name']) + ': ' + string (Table1 ['Population']));

end;


The button below changes the Alignment of every field:


procedure TForm2.Btn3Click

(Sender: TObject);

var

        I: Integer;

begin

   for I := 0 to Table1.FieldCount - 1 do

        Table1.Fields[I].Alignment :=

        taCenter;

end;


code below checks a field to see if it's null or zero(0) or empty:


procedure TForm2.Table1CalcField

(DataSet: TDataSet);

begin

    IF not Table1Area.IsNull and

        (Table1Area.Value <> 0)

        then


        Table1PopulationDen.Value :=

        Table1Population.Value /

        Table1Area.Value

   else

        Table1PopulationDen.Value := 0;        

   end;