Accessing Field Values with a Dataset's Fields Property

From RAD Studio
Jump to: navigation, search

Go Up to Displaying, Converting, and Accessing Field Values


You can access the value of a field with the Fields property of the dataset component to which the field belongs. Fields maintains an indexed list of all the fields in the dataset. Accessing field values with the Fields property is useful when you need to iterate over a number of columns, or if your application works with tables that are not available to you at design time.

To use the Fields property you must know the order of and data types of fields in the dataset. You use an ordinal number to specify the field to access. The first field in a dataset is numbered 0. Field values must be converted as appropriate using each field component's conversion properties.

For example, the following statement assigns the current value of the seventh column (Country) in the Customers table to an edit control:

Delphi:

Edit1.Text := CustTable.Fields[6].AsString;

C++:

Edit1->Text = CustTable->Fields->Fields[6]->AsString;

Conversely, you can assign a value to a field by setting the Fields property of the dataset to the desired field. For example:

Delphi:

Customers.Edit;
Customers.Fields[6].AsString := Edit1.Text;
Customers.Post;

C++:

Customers->Edit();
Customers->Insert();
Customers->Fields->Fields[6]->AsString = Edit1->Text;
Customers->Post();

See Also