DBGrid Control

Hierarchy: TObject - TPersistent - TComponent - TControl - TWinControl - TCustomControl - TCustomGrid - TCustomDBGrid - TDBGrid

The DBGrid component is used to display records from a table in rows. 

In this example there are three forms, two of which have been displayed as data grids so that more than one record can be seen at a time. The logic of this display is that the first data grid shows invoice items related to the invoice displayed at the top of the form while the second grid displays details of items that can be placed in the first grid.

A DBGrid component needs to take its data from a DataSource component, whose properties will link in turn to a database source component such as an ADOTable. The fields in a DBGrid are derived from these links to the data source. In ADO the fields can be obtained by double clicking the ADOTable component to which the DataSource for the grid is attached and then right-clicking the field window and choosing 'Add All Fields':

When a field in the list is selected its properties are displayed:

For the Total Cost field the DisplayFormat property has been amended to "'£'#.00". There is also a visible property that can determine whether or not the field is visible in the grid display. Properties of individual fields are based on the TField object, which has many properties and methods for changing, converting and validating field data, for defining the appearance of data, for calculating field values and for looking up values from another dataset.

Example

In this short example the code clears the contents of a cell when it is double-clicked :

procedure TForm1.DBGrid1CellClick(Column: TColumn);
begin
adotable1.Edit;
dbgrid1.SelectedField.Text:=' ';
end;

This might be used in something like a booking system to set and clear reservations.

Back to Tutorial

Back to Palette List