dbgrid_在DBGrid中创建下拉列表

dbgrid

Want to make the best data editing grid ever? Below are instructions for building a user interface for editing lookup fields Inside a DBGrid. Specifically, we'll be looking at how to place a DBLookupComboBox into a cell of a DBGrid.

是否想成为有史以来最好的数据编辑网格? 以下是建立用户界面以编辑DBGrid内的查找字段的说明 。 具体来说,我们将研究如何将DBLookupComboBox放入DBGrid的单元格中。

What this will do is call upon information from a data source that will be used to populate a drop-down box.

这将需要来自数据源的信息,该信息将用于填充下拉框。

To show a DBLookupComboBox inside a cell of a DBGrid, you first need to make one available at run time...

要在DBGrid的单元格中显示一个DBLookupComboBox,您首先需要在运行时使其可用...

使用DBLookupComboBox创建查找 ( Create a Lookup With a DBLookupComboBox )

Select the "Data controls" page on the Component Palette and pick a DBLookupComboBox. Drop one anywhere on the form and leave the default name of "DBLookupComboBox1." It doesn't matter where you put it since most of the time, it will be invisible or floating over the grid.

在“组件面板”上选择“数据控件”页面,然后选择一个DBLookupComboBox。 在窗体上的任意位置放置一个,并保留默认名称“ DBLookupComboBox1”。 自从大多数时候以来,放置在哪里都无所谓,它将不可见或漂浮在网格上。

Add one more DataSource and DataSet component to "fill" the combo box with values. Drop a TDataSource (with the name DataSource2) and TAdoQuery (name it AdoQuery1) anywhere on the form.

再添加一个DataSource和DataSet组件以用值“填充”组合框。 在窗体上的任意位置放置一个TDataSource(名称为DataSource2)和TAdoQuery(名称为AdoQuery1)。

For a DBLookupComboBox to work properly, several more properties must be set; they're the key to the lookup connection:

为了使DBLookupComboBox正常工作,必须设置更多属性。 它们是查找连接的关键:

  • DataSource and DataField determine the main connection. The DataField is a field into which we insert the looked-up values.

    DataSourceDataField确定主要连接。 DataField是一个字段,我们将在其中插入查询值。

  • ListSource is the source of the lookup dataset.

    ListSource是查找数据集的源。

  • KeyField identifies the field in the ListSource that must match the value of the DataField field.

    KeyField标识ListSource中的字段,该字段必须与DataField字段的值匹配。

  • ListFields is the field(s) of the lookup dataset that are actually displayed in the combo. ListField can show more than one field but multiples should be separated by semicolons.

    ListFields是在组合中实际显示的查找数据集的字段。 ListField可以显示多个字段,但多个字段应用分号分隔。

    You have to set large enough value for the

    您必须为

    DropDownWidth (of a ComboBox) to really see multiple columns of data.

    (ComboBox的) DropDownWidth可以真正看到多列数据。

    Here's how to set all the important properties from code (in the form's

    以下是通过代码设置所有重要属性的方法(在表单的

    OnCreate event handler):

    OnCreate事件处理程序):


procedure TForm1.FormCreate(Sender: TObject);beginwith DBLookupComboBox1 dobegin
DataSource := DataSource1; // -> AdoTable1 -> DBGrid1
ListSource := DataSource2;
DataField := 'AuthorEmail'; // from AdoTable1 - displayed in the DBGrid
KeyField := 'Email';
ListFields := 'Name; Email';
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, like in the above example, you have to make sure that all columns are visible. This is done by setting the DropDownWidth property.

注意:当您想要在DBLookupComboBox中显示多个字段时,如上例所示,您必须确保所有列均可见。 这是通过设置DropDownWidth属性来完成的。

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.

但是,您会看到,最初必须将其设置为非常大的值,这会导致丢弃列表太宽(在大多数情况下)。 一种解决方法是设置下拉列表中显示的特定字段的DisplayWidth。

This code, placed inside the OnCreate event for the form, ensures that both the author name and it's email are displayed inside the drop-down list:

此代码位于表单的OnCreate事件中,可确保作者名称及其电子邮件均显示在下拉列表中:


AdoQuery1.FieldByName('Email').DisplayWidth:=10;
AdoQuery1.FieldByName('Name').DisplayWidth:=10;
AdoQuery1.DropDownWidth:=150;

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. First, we need to make sure the DBLookupComboBox1 is moved and sized over the cell in which the AuthorEmail field is displayed.

剩下要做的是实际使组合框悬停在单元格上(处于编辑模式时),显示AuthorEmail字段。 首先,我们需要确保在显示AuthorEmail字段的单元格上移动DBLookupComboBox1并调整其大小。


procedure TForm1.DBGrid1DrawColumnCell
(Sender: TObject;
const Rect: TRect;
DataCol: Integer;
Column: TColumn;
State: TGridDrawState);beginif (gdFocused in State) thenbeginif (Column.Field.FieldName = DBLookupComboBox1.DataField) thenwith 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;endend;

Next, when we leave the cell, we have to hide the combo box:

接下来,当我们离开单元格时,我们必须隐藏组合框:


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

Note that when in editing mode, all keystrokes are going to the DBGrid's cell but we have to make sure they are sent to the DBLookupComboBox. In the case of a DBLookupComboBox, we are primarily interested in the [Tab] key; it should move the input focus to the next cell.

请注意,在编辑模式下,所有击键都将进入DBGrid的单元格,但是我们必须确保将其发送到DBLookupComboBox。 对于DBLookupComboBox,我们首先对[Tab]键感兴趣; 它将输入焦点移到下一个单元格。


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

When you pick an item ("row") from a DBLookupComboBox, the value or the corresponding KeyField field is stored as the value of the DataField field.

当您从DBLookupComboBox中选择一项(“行”)时,该值或相应的KeyField字段将存储为DataField字段的值。

翻译自: https://www.thoughtco.com/place-dblookupcombobox-into-dbgrid-4077834

dbgrid

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值