Читайте также:
|
|
Lecture 8. Displaying data with standard controls
You can display database values at run time with standard components as well as data aware and TField components. Besides accessing TField components created with the Fields Editor, there are two ways to access column values at run time: the Fields property and the FieldsByName method. Each accesses the value of the current row of the specified column in the underlying database table at run time. Each requires a dataset component in the form, but not a TDataSource component.
In general, you should use the data-aware controls built in to Delphi in database applications. These components have properties and methods built in to them that enable them to be connected to database columns, display the current values in the columns, and make updates to the columns. If you use standard components, you must provide analogous code by hand.
Using the Fields property
You can access the value of a field with the Fields property of a dataset component, using as a parameter the ordinal number of the column in the table (starting at 0). To access or change the field’s value, convert the result with the appropriate conversion function, such as AsString or AsInteger.
This method requires you to know the order and data types of the columns in the table. Use this method if you want to iterate over a number of columns or if your application works with tables that are not available at design time.
For example, the following statement assigns the current value of the seventh column (Country) in the CustTable table to the Edit1 component:
Edit1.Text:= CustTable.Fields[6].AsString;
Conversely, you can assign a value to a column a dataset in Edit mode by assigning the appropriate Fields property to the value of a component. For example,
CustTable.Fields[6].AsString:= Edit1.Text;
To make these assignments occur, you must enter them in an event such as a TButton OnClick event, or an edit control’s OnEnter event.
Using the FieldByName method
You can access the value of a field with the FieldByName method by specifying the dataset component name, and then passing FieldByName the name of the column you want to access. To access or change the field’s value, convert the result with the appropriate conversion function, such as AsString or AsInteger.
This method requires you to know the name of the column you want to access or if your application works with tables that are not available at design time.
For example, the following statement assigns the value of the Country column in the CustTable table to Edit2:
Edit2.Text:= CustTable.FieldByName('Country').AsString;
Conversely, you can assign a value to a column a dataset in Edit mode by assigning the appropriate FieldByName property to the value of a component. For example,
CustTable.FieldByName('Country').AsString:= Edit2.Text;
To make these assignments occur, you must enter them in an event such as a button’s OnClick or a Edit component’s OnExit event.
Дата добавления: 2015-10-26; просмотров: 144 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
ДИСКУССИЯ С УЧИТЕЛЯМИ | | | Data type mappings |