如何在DBGrid中使用复选框

There are numerous ways and reasons to customize the output of a DBGrid in Delphi. One way is to add checkboxes so that the result is more visually attractive.

Delphi中自定义DBGrid的输出有很多方法和理由。 一种方法是添加复选框,以使结果在视觉上更具吸引力。

By default, if you have a boolean field in your dataset, the DBGrid displays them as "True" or "False" depending on the value of the data field. However, it looks much better if you choose to use a "true" checkbox control to enable editing the fields.

默认情况下,如果数据集中有一个布尔字段,则DBGrid 根据数据字段的值将它们显示为“ True”或“ False”。 但是,如果您选择使用“ true”复选框控件来启用编辑字段,则看起来会好得多。

创建一个示例应用程序 ( Create a Sample Application )

Start a new form in Delphi, and place a TDBGrid, TADOTable, and TADOConnection, TDataSource.

在Delphi中启动一个新表单,并放置一个TDBGrid,TADOTable和TADOConnection,TDataSource。

Leave all the component names as they are when they were first dropped into the form (DBGrid1, ADOQuery1, AdoTable1, etc.). Use the Object Inspector to set a ConnectionString property of the ADOConnection1 component (TADOConnection) to point to the sample QuickiesContest.mdb MS Access database.

保留所有组件名称的名称(当它们首次放入表单时(DBGrid1,ADOQuery1,AdoTable1等))。 使用对象检查器设置ADOConnection1组件(TADOConnection)的ConnectionString属性,使其指向示例QuickiesContest.mdb MS Access数据库。

Connect DBGrid1 to DataSource1, DataSource1 to ADOTable1, and finally ADOTable1 to ADOConnection1. The ADOTable1 TableName property should point to the Articles table (to make the DBGrid display the records of the Articles table).

将DBGrid1连接到DataSource1,将DataSource1连接到ADOTable1,最后将ADOTable1连接到ADOConnection1。 ADOTable1 TableName属性应指向Articles表(以使DBGrid显示Articles表的记录)。

If you have set all the properties correctly, when you run the application (given that the Active property of the ADOTable1 component is True) you should see, by default, the DBGrid display the boolean field's value as "True" or "False" depending on the value of the data field.

如果您正确设置了所有属性,则在运行应用程序时(假设ADOTable1组件的Active属性为True),默认情况下,DBGrid将显示布尔字段的值为“ True”或“ False”,具体取决于数据字段的值。

DBGrid中的复选框 ( CheckBox in a DBGrid )

To show a checkbox inside a cell of a DBGrid, we'll need to make one available for us at run time.

要在DBGrid的单元格中显示一个复选框,我们需要在运行时为我们提供一个复选框。

Select the "Data controls" page on the Component Palette and pick a TDBCheckbox. Drop one anywhere on the form - it doesn't matter where, since most of the time it will be invisible or floating over the grid.

在“ 组件面板 ”上选择“数据控件”页面,然后选择一个TDBCheckbox 。 在表单上的任意位置放一个-没关系,因为在大多数情况下它将不可见或漂浮在网格上。

Tip: TDBCheckBox is a data-aware control that allows the user to select or deselect a single value, which is appropriate for boolean fields.

提示: TDBCheckBox是一个数据感知控件,允许用户选择或取消选择单个值,该值适用于布尔字段。

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.

接下来,将其Visible属性设置为False。 将DBCheckBox1的Color属性更改为与DBGrid相同的颜色(以便它与DBGrid融合在一起)并删除标题。

Most importantly, make sure the DBCheckBox1 is connected to the DataSource1 and to the correct field.

最重要的是,确保DBCheckBox1已连接到DataSource1和正确的字段。

Note that all the above DBCheckBox1's property values can be set in the form's OnCreate event like this:

请注意,可以在窗体的OnCreate事件中设置上述所有DBCheckBox1的属性值,如下所示:


procedure TForm1.FormCreate(Sender: TObject);begin
DBCheckBox1.DataSource := DataSource1;
DBCheckBox1.DataField := 'Winner';
DBCheckBox1.Visible := False;
DBCheckBox1.Color := DBGrid1.Color;
DBCheckBox1.Caption := '';//explained later in the article
DBCheckBox1.ValueChecked := 'Yes a Winner!';
DBCheckBox1.ValueUnChecked := 'Not this time.'; end;

What comes next is the most interesting part. While editing the boolean field in the DBGrid, we need to make sure the DBCheckBox1 is placed above ("floating") the cell in the DBGrid displaying the boolean field.

接下来是最有趣的部分。 在编辑DBGrid中的布尔值字段时,我们需要确保将DBCheckBox1放置在显示布尔值字段的DBGrid单元格上方(“浮动”)。

For the rest of the (non-focused) cells carrying the boolean fields (in the "Winner" column), we need to provide some graphical representation of the boolean value (True/False). This means you need at least two images for drawing: one for the checked state (True value) and one for the unchecked state (False value).

对于带有布尔字段的其余(非焦点)单元格(在“优胜者”列中),我们需要提供布尔值(真/假)的一些图形表示。 这意味着您至少需要绘制两张图像:一张用于检查状态(“真”值),另一张用于未检查状态(“假”值)。

The easiest way to accomplish this is to use the Windows API DrawFrameControl function to draw directly on the DBGrid's canvas.

完成此操作的最简单方法是使用Windows API DrawFrameControl函数直接在DBGrid的画布上绘制。

Here's the code in the DBGrid's OnDrawColumnCell event handler that occurs when the grid needs to paint a cell.

这是DBGrid的OnDrawColumnCell事件处理程序中的代码,当网格需要绘制单元格时发生。


procedure TForm1.DBGrid1DrawColumnCell(
Sender: TObject; const Rect: TRect; DataCol:
Integer; Column: TColumn; State: TGridDrawState);const IsChecked : array[Boolean] of Integer =
(DFCS_BUTTONCHECK, DFCS_BUTTONCHECK or DFCS_CHECKED);var
DrawState: Integer;
DrawRect: TRect;beginif (gdFocused in State) thenbeginif (Column.Field.FieldName = DBCheckBox1.DataField) thenbegin
DBCheckBox1.Left := Rect.Left + DBGrid1.Left + 2;
DBCheckBox1.Top := Rect.Top + DBGrid1.top + 2;
DBCheckBox1.Width := Rect.Right - Rect.Left;
DBCheckBox1.Height := Rect.Bottom - Rect.Top;
DBCheckBox1.Visible := True;endendelsebeginif (Column.Field.FieldName = DBCheckBox1.DataField) thenbegin
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:

要完成此步骤,我们需要确保在离开单元格时DBCheckBox1不可见:


procedure TForm1.DBGrid1ColExit(Sender: TObject);beginif DBGrid1.SelectedField.FieldName = DBCheckBox1.DataField then 
DBCheckBox1.Visible := Falseend;

We need just two more events to handle.

我们只需要再处理两个事件。

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 CheckBox. In the case of a CheckBox we are primarily interested in the [Tab] and the [Space] key. [Tab] should move the input focus to the next cell, and [Space] should toggle the state of the CheckBox.

请注意,在编辑模式下,所有击键都将进入DBGrid的单元格,我们必须确保将它们发送到CheckBox。 对于CheckBox,我们主要对[Tab]和[Space]键感兴趣。 [Tab]应该将输入焦点移到下一个单元格,[Space]应该切换CheckBox的状态。


procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);beginif (key = Chr(9)) then Exit;if (DBGrid1.SelectedField.FieldName = DBCheckBox1.DataField) thenbegin
DBCheckBox1.SetFocus;
SendMessage(DBCheckBox1.Handle, WM_Char, word(Key), 0);end;end;

It could be appropriate for the Caption of the checkbox to change as the user checks or unchecks the box. Note that the DBCheckBox has two properties (ValueChecked and ValueUnChecked) used to specify the field value represented by the checkbox when it is checked or unchecked.

当用户选中或取消选中复选框时,更改标题的标题可能是适当的。 请注意,DBCheckBox具有两个属性(ValueChecked和ValueUnChecked),用于指定复选框选中或未选中时表示的字段值。

This ValueChecked property holds "Yes, a Winner!", and ValueUnChecked equals "Not this time."

此ValueChecked属性持有“是,赢家!”,而ValueUnChecked等于“不是这次”。


procedure TForm1.DBCheckBox1Click(Sender: TObject);beginif DBCheckBox1.Checked then
DBCheckBox1.Caption := DBCheckBox1.ValueCheckedelse
DBCheckBox1.Caption := DBCheckBox1.ValueUnChecked;end;

Run the project and you'll see the checkboxes all over the Winner field's column.

运行项目,您将在“获胜者”字段的所有列中看到复选框。

翻译自: https://www.thoughtco.com/place-a-checkbox-into-dbgrid-4077440

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值