Professional Documents
Culture Documents
Next, set its Visible property to False. Change the Color property of DBCheckBox1 to
the same color as the DBGrid (so it blends in with the DBGrid) and remove the
Caption. And most importantly, make sure the DBCheckBox1 is connected to the
DataSource1 and to the correct field (DataSource = DataSource1, DataField =
Winner).
Note that all the above DBCheckBox1's property values can be set in the form's
OnCreate event like:
:= False;
DBCheckBox1.Color
:= DBGrid1.Color;
DBCheckBox1.Caption
:= '';
graphical representation of the boolean value (True/False). This means you need at
least two images for drawing: one for the checked (True value) state, and one for
the unchecked (False value) state. The easiest way to accomplish this is to use the
Windows API DrawFrameControl function to draw directly on the DBGrid's canvas.
Here's the code in the DBGrid's OnDrawColumnCell event handler that occurs when
the grid needs to paint a cell.
procedure TForm1.DBGrid1DrawColumnCell(
Sender: TObject; const Rect: TRect; DataCol:
Integer; Column: TColumn; State: TGridDrawState);
DBCheckBox1.Visible := True;
end
end
else
begin
if (Column.Field.FieldName = DBCheckBox1.DataField) then
begin
DrawRect:=Rect;
InflateRect(DrawRect,-1,-1);
DrawState := ISChecked[Column.Field.AsBoolean];
DBGrid1.Canvas.FillRect(Rect);
DrawFrameControl(DBGrid1.Canvas.Handle, DrawRect,
DFC_BUTTON, DrawState);
end;
end;
end;
To finish this step, we need to make sure DBCheckBox1 is invisible when we leave
the cell:
Visible
:= False;
end;
DataSource2.DataSet := AdoQuery1;
AdoQuery1.Connection := AdoConnection1;
AdoQuery1.SQL.Text := 'SELECT Name, Email FROM Authors';
AdoQuery1.Open;
end;
Note: when you want to display more than one field in a DBLookupComboBox, as in
the example above, you have to make sure that all columns are visible. This is done
by setting the DropDownWidth property. However, you'll see that initially you have
to set this to a very large value which results in dropped list being too wide in most
cases. One workaround is to set the DisplayWidth of a particular Field shown in a
drop down list. The next code snippet, placed inside the OnCreate event for the
form, ensures that both author name and it's email are displayed inside the drop
down list:
AdoQuery1.FieldByName('Email').DisplayWidth:=10;
AdoQuery1.FieldByName('Name').DisplayWidth:=10;
AdoQuery1.DropDownWidth:=150;
Magic...
What's left for us to do, is to actually make a combo box hover over a cell (when in
edit mode) displaying the AuthorEmail field. We've already discussed the theory - I'll
show you only the code here (you'll have the option to download the entire project
later):
First, we need to make sure the DBLookupComboBox1 is moved (and sized) over the
cell in which the AuthorEmail field is displayed.
procedure TForm1.DBGrid1DrawColumnCell
(Sender: TObject;
const Rect: TRect;
DataCol: Integer;
Column: TColumn;
State: TGridDrawState);
begin
if (gdFocused in State) then
begin
if (Column.Field.FieldName = DBLookupComboBox1.DataField) then
with DBLookupComboBox1 do
begin
Left := Rect.Left + DBGrid1.Left + 2;
Top := Rect.Top + DBGrid1.Top + 2;
Width := Rect.Right - Rect.Left;
Width := Rect.Right - Rect.Left;
Height := Rect.Bottom - Rect.Top;
Visible := True;
end;
end
end;
Next, when we leave the cell, we have to hide the combo box:
DateTimePicker in a DBGrid!
procedure TForm1.DBGrid1DrawColumnCell
(Sender: TObject;
const Rect: TRect;
DataCol: Integer;
Column: TColumn;
State: TGridDrawState);
begin
if (gdFocused in State) then
begin
if (Column.Field.FieldName = 'DateAdded') then
with DateTimePicker do
begin
Left := Rect.Left + DBGrid1.Left + 1;
Top := Rect.Top + DBGrid1.Top + 1;
Width := Rect.Right - Rect.Left + 2;
Width := Rect.Right - Rect.Left + 2;
Height := Rect.Bottom - Rect.Top + 2;
Visible := True;
end;
end
end;
Next, when we leave the cell, we have to hide the date time picker:
Next, note that when in editing mode, all keystrokes are going to the DBGrid's cell,
we have to make sure they are sent to the DateTimePicker. We are primarily
interested in the [Tab] key - [Tab] should move the input focus to the next cell.
procedure TForm1.DBGrid1KeyPress
(Sender: TObject; var Key: Char);
begin
if (key = Chr(9)) then Exit;