I work with Delphi
I created and added a boolean field to tAdoQuery at runtime, and it doesn't exist in the database.
I want to know how to edit the field for one and for many records.
Create detailed examples demonstrating all that
Assistant
User
how to display boolean field as checkbox in dbgrid professtionly in delphi
Assistant
In Delphi, when you add a boolean (or any) field at runtime to a TADOQuery dataset that does not exist in the database (a calculated or persistent field in-memory), the field is not linked to the database but exists only in the client dataset buffer.
To edit and update that boolean field for one or many records, you need to work with the TField values in the TADOQuery records while they are in edit mode. Since the field is not bound to the database, changes to the field will only be persisted in the dataset during the lifetime of the dataset (in memory).
Step 1: Add a Boolean Field to TADOQuery at Runtime
delphi
var
BoolField: TBooleanField;
begin
BoolField := TBooleanField.Create(AdoQuery1);
BoolField.FieldName := 'MyBooleanField'; // Your new field name
BoolField.DisplayName := 'My Boolean Field';
BoolField.DataSet := AdoQuery1; // Link to dataset
BoolField.FieldKind := fkData; // Normal data field (not calculated/lookup)
BoolField.ReadOnly := False;
BoolField.DefaultExpression := 'False'; // Default value (in-memory only)
BoolField.Visible := True;
BoolField.Value := False; // Initial Value
end;
This field does not exist in the database but is part of the dataset fields and accessible through AdoQuery1.FieldByName('MyBooleanField').
Step 2: Modify the Field Value for a Single Record
If you want to edit the field for the current record, do:
delphi
procedure SetBooleanFieldForCurrentRecord(Query: TADOQuery; const FieldName: string; Value: Boolean);
begin
if not Query.Eof then
begin
Query.Edit; // Put into edit mode
Query.FieldByName(FieldName).AsBoolean := Value;
Query.Post; // Save changes to the buffer
end
else
ShowMessage('No current record');
end;
FieldKind: Set to fkData to allow editing. Other options include fkCalculated or fkLookup but those are special cases.
Field and Dataset State: You must put the dataset into edit mode before modifying data (Query.Edit), and after change call Query.Post.
No Database Update for This Field: Since the field is created at runtime and does not exist in your database, it will never be saved back to the database by Post. It only exists in memory inside the dataset.
Persisting Data: If you want to save that boolean value permanently for multiple records, you'd have to either:
Store the values in some database table field by extending your table
Maintain it separately (e.g., external file, in-memory mapping)
Full Example in Context
delphi
procedure TForm1.FormCreate(Sender: TObject);
var
BoolField: TBooleanField;
begin
AdoQuery1.Close;
AdoQuery1.SQL.Text := 'SELECT ID, Name FROM MyTable'; // Original DB query
AdoQuery1.Open;
// Create boolean field at runtime
BoolField := TBooleanField.Create(AdoQuery1);
BoolField.FieldName := 'MyBooleanField';
BoolField.DataSet := AdoQuery1;
BoolField.FieldKind := fkData;
BoolField.ReadOnly := False;
BoolField.DefaultExpression := 'False';
BoolField.Visible := True;
// Initialize new boolean field to False for all records
SetBooleanFieldForAllRecords(AdoQuery1, 'MyBooleanField', False);
end;
procedure TForm1.ButtonSetCurrentTrueClick(Sender: TObject);
begin
SetBooleanFieldForCurrentRecord(AdoQuery1, 'MyBooleanField', True);
end;
procedure TForm1.ButtonSetAllTrueClick(Sender: TObject);
begin
SetBooleanFieldForAllRecords(AdoQuery1, 'MyBooleanField', True);
end;
This will create a boolean field in-memory for the query and you can modify it per-record or for all records during runtime.
Let me know if you want to also see how to synchronize this with an actual database field!
Displaying a Boolean field as a checkbox professionally in a TDBGrid in Delphi is a very common requirement and Delphi supports this quite nicely. Here's a detailed explanation and example of how to do it:
How TDBGrid Displays Boolean Fields
By default, if your dataset has a TBooleanField or TWordBoolField or any boolean field, and you place a TDBGrid on the form, it will render that boolean field as a checkbox automatically — if you haven't overridden the column rendering.
However, if:
You added the boolean field at runtime, or
You want to customize the checkbox behavior or appearance,
Or you want consistent, professional appearance (fixed checkbox size, alignments, and no literal text 'True'/'False'),
then you should ensure the column is properly configured.
Steps to Show a Boolean Field as Checkbox in TDBGrid Professionally
1) Make sure the field is of type TBooleanField
If you created the boolean field manually at runtime, as shown in the previous example:
TBooleanField is the right class that TDBGrid knows to render as a checkbox column.
2) Set up TDBGrid Columns explicitly (optional but professional)
When your TDBGrid uses AutoGenerateColumns := True (the default), it creates columns automatically and usually renders boolean fields as checkbox columns. But sometimes it doesn't behave perfectly.
To gain full control, set AutoGenerateColumns to False, and manually create columns for the grid, and for your boolean field specifically, set the column's ColumnType to checkbox.
3) Create and configure the boolean column as TColumn with Checkbox
Here is an example showing how to create columns for TDBGrid at runtime and explicitly tell the column for the boolean field to display checkboxes:
delphi
procedure SetupGridColumns(AGrid: TDBGrid; ADataSet: TDataSet);
var
I: Integer;
Col: TColumn;
begin
AGrid.Columns.Clear;
for I := 0 to ADataSet.FieldCount - 1 do
begin
Col := AGrid.Columns.Add;
Col.FieldName := ADataSet.Fields[I].FieldName;
// If this is a boolean field, set checkbox style
if ADataSet.Fields[I] is TBooleanField then
begin
Col.ButtonStyle := cbsAuto; // enables checkbox display & toggle
Col.Alignment := taCenter; // center checkbox for better look
Col.Width := 50; // reasonable width for checkbox column
end
else
begin
Col.ButtonStyle := cbsNone;
Col.Alignment := taLeftJustify;
Col.Width := 100;
end;
end;
end;
4) Wire it up
In your form initialization, after opening the dataset:
delphi
procedure TForm1.FormCreate(Sender: TObject);
begin
AdoQuery1.Open;
// Optional: Add boolean field at runtime (if needed, see previous examples)
// ...
// Disable automatic column generation
DBGrid1.AutoGenerateColumns := False;
// Setup columns manually with checkbox column for boolean field
SetupGridColumns(DBGrid1, AdoQuery1);
end;
5) Behavior and Appearance Tips
Checkbox is editable by default if the dataset is editable and field is not read-only.
Use DBGrid1.Options to control grid behavior:
dgEditing should be included in Options to allow checking/unchecking.
dgAlwaysShowEditor can be disabled so clicking toggles checkbox immediately.
You can also customize the colors with DBGrid1.Style or via OnDrawColumnCell event, but this usually isn’t necessary for checklist display.
Summary
Correct TBooleanField type must be present in dataset
Set TDBGrid.AutoGenerateColumns := False
Create columns manually and set for boolean field columns: ButtonStyle := cbsAuto and Alignment := taCenter
Ensure dgEditing is enabled in grid's Options to allow checkbox toggle
Complete Minimal Example
delphi
uses
DB, ADODB;
procedure TForm1.FormCreate(Sender: TObject);
var
BoolField: TBooleanField;
procedure SetupGridColumns;
var
I: Integer;
Col: TColumn;
begin
DBGrid1.Columns.Clear;
for I := 0 to AdoQuery1.FieldCount - 1 do
begin
Col := DBGrid1.Columns.Add;
Col.FieldName := AdoQuery1.Fields[I].FieldName;
if AdoQuery1.Fields[I] is TBooleanField then
begin
Col.ButtonStyle := cbsAuto;
Col.Alignment := taCenter;
Col.Width := 50;
end
else
begin
Col.ButtonStyle := cbsNone;
Col.Alignment := taLeftJustify;
Col.Width := 100;
end;
end;
end;
begin
AdoQuery1.Close;
AdoQuery1.SQL.Text := 'SELECT ID, Name FROM MyTable';
AdoQuery1.Open;
BoolField := TBooleanField.Create(AdoQuery1);
BoolField.FieldName := 'Selected';
BoolField.DataSet := AdoQuery1;
BoolField.FieldKind := fkData;
BoolField.ReadOnly := False;
BoolField.DefaultExpression := 'False';
BoolField.Visible := True;
// Initialize checkbox field for all records
AdoQuery1.DisableControls;
try
AdoQuery1.First;
while not AdoQuery1.Eof do
begin
AdoQuery1.Edit;
AdoQuery1.FieldByName('Selected').AsBoolean := False;
AdoQuery1.Post;
AdoQuery1.Next;
end;
finally
AdoQuery1.EnableControls;
end;
DBGrid1.AutoGenerateColumns := False;
SetupGridColumns;
DBGrid1.Options := DBGrid1.Options + [dgEditing];
end;
Additional Notes
If you want to make the checkbox read-only, you can set the field to ReadOnly := True.
To further customize checkbox painting, intercept the OnDrawColumnCell event, but this is rarely needed, because cbsAuto handles rendering internally.
Works with VCL standard TDBGrid. For third-party grids, check their documentation for proper checkbox column setup.
Let me know if you want examples with custom painting or with alternative grids like DevExpress or TMS—for those there are specific components and methods to display bool columns as checkboxes!