dbgrid_如何将下拉选择列表放入DBgrid

dbgrid

Here's how to place a drop-down pick list into a DBGrid. Create visually more attractive user interfaces for editing lookup fields inside a DBGrid - using the PickList property of a DBGrid column.

这是将下拉选择列表放入DBGrid的方法。 使用DBGrid列的PickList属性,创建视觉上更具吸引力的用户界面,以编辑DBGrid内部的查找字段。

Now, that you know what are lookup fields, and what are the options of displaying a lookup field in Delphi's DBGrid, it's time to see how to use the PickList property of a DGBrid column to enable a user to pick a value for a lookup field from a drop-down list box.

现在,您知道什么是查找字段,以及在Delphi的DBGrid中显示查找字段的选项是什么,现在该看看如何使用DGBrid列的PickList属性来使用户能够为从下拉列表框中查找字段。

DBGrid Columns属性的快速信息 ( A Quick Info on DBGrid Columns Property )

A DBGrid control has a Columns property - a collection of TColumn objects representing all of the columns in a grid control. Columns can be set at design time through the Columns editor, or programmatically at runtime. You'll usually add Columns to a DBGird when you want to define how a column appears, how the data in the column is displayed and to access the properties, events, and methods of TDBGridColumns at runtime. A customized grid enables you to configure multiple columns to present different views of the same dataset (different column orders, different field choices, and different column colors and fonts, for example).

DBGrid控件具有Columns属性-代表网格控件中所有列的TColumn对象的集合。 可以在设计时通过“列”编辑器设置列,也可以在运行时以编程方式设置列。 当您想要定义列的显示方式,列的数据显示方式以及在运行时访问TDBGridColumns的属性,事件和方法时,通常将添加到DBGird中。 使用定制的网格,您可以配置多个列以显示同一数据集的不同视图(例如,不同的列顺序,不同的字段选择以及不同的列颜色和字体)。

Now, each Column in a grid is "linked" to a field from a dataset displayed in the grid. What's more, each column has a PickList property. The PickList property lists values that the user can select for the column's linked field value.

现在,网格中的每个列都“链接”到网格中显示的数据集中的字段。 而且,每列都有一个PickList属性。 PickList属性列出用户可以为列的链接字段值选择的值。

填写选择清单 ( Filling the PickList )

What you will learn here is how to fill that String List with values from another dataset at run time.Recall, that we are editing the Articles table and that a Subject field can only accept values from the Subjects table: the ideal situation for the PickList!

您将在这里学到的是如何在运行时用另一个数据集的值填充该字符串列表,回想一下,我们正在编辑Articles表,并且Subject字段只能接受Subjects表中的值:PickList的理想情况!

Here's how to set up the PickList property. First, we add a call to the SetupGridPickList procedure in the Form's OnCreate event handler.

这是设置PickList属性的方法。 首先,我们在窗体的OnCreate事件处理程序中添加对SetupGridPickList过程的调用。


procedure TForm1.FormCreate(Sender: TObject);begin
SetupGridPickList('Subject', 'SELECT Name FROM Subjects');end;

The easiest way to create the SetupGridPickList procedure is to go to the private part of the form declaration, add the declaration there and hit the CTRL + SHIFT + C key combination - Delphi's code completion will do the rest:

创建SetupGridPickList过程的最简单方法是转到表单声明的私有部分,在该声明中添加声明,然后按CTRL + SHIFT + C键组合-Delphi的代码完成将完成其余工作:


...type
TForm1 = class(TForm)
...privateprocedure SetupGridPickList(const FieldName : string; const sql : string);public
...

Note: the SetupGridPickList procedure takes two parameters. The first parameter, FieldName, is the name of the field we want to act like a lookup field; the second parameter, SQL, is the SQL expression we use to populate the PickList with possible values - in general, the SQL expression should return a dataset with only one field.

注意:SetupGridPickList过程采用两个参数。 第一个参数FieldName是我们要充当查找字段的字段的名称; 第二个参数SQL是我们用来在PickList中填充可能值SQL表达式-通常,SQL表达式应返回仅包含一个字段的数据集。

Here's how the SetupGridPickList looks like:

这是SetupGridPickList的样子:


procedure TForm1.SetupGridPickList(const FieldName, sql: string);var 
slPickList:TStringList;
Query : TADOQuery;
i : integer;begin
slPickList:=TStringList.Create;
Query := TADOQuery.Create(self);try
Query.Connection := ADOConnection1;
Query.SQL.Text := sql;
Query.Open;//Fill the string listwhile not Query.EOF dobegin
slPickList.Add(Query.Fields[0].AsString);
Query.Next;end; //while//place the list it the correct columnfor i:=0 to DBGrid1.Columns.Count-1 do if DBGrid1.Columns[i].FieldName = FieldName thenbegin
DBGrid1.Columns[i].PickList:=slPickList;
Break;end;finally
slPickList.Free;
Query.Free;end; end; (*SetupGridPickList*)

That's it. Now, when you click the Subject column (to enter into edit mode).

而已。 现在,当您单击“主题”列(进入编辑模式)时。

Note 1: by default, the drop-down list displays 7 values. You can change the length of this list by setting the DropDownRows property.

注意1:默认情况下,下拉列表显示7个值。 您可以通过设置DropDownRows属性来更改此列表的长度。

Note 2: nothing stops you from filling up the PickList from a list of values not coming from a database table. If, for example, you have a field that only accepts weekday names ('Monday', ..., 'Sunday') you can build a "hard-coded" PickList.

注意2:没有什么可以阻止您从非数据库表的值列表中填充PickList。 例如,如果您有一个仅接受工作日名称的字段(“星期一”,...,“星期日”),则可以构建“硬编码” PickList。

"Uh, I need to click the PickList 4 times..."

“嗯,我需要单击PickList 4次...”

Note that when you want to edit the field displaying a drop-down list, you'll need to click the cell 4 times in order to actually pick a value from a list. The next code snippet, added to the DBGrid's OnCellClick event handler, mimics a hit to the F2 key followed by Alt + DownArrow.

请注意,当您要编辑显示下拉列表的字段时,需要单击4次以实际从列表中选择一个值。 添加到DBGrid的OnCellClick事件处理程序中的下一个代码段模仿了F2键的命中,然后按下Alt + DownArrow。


procedure TForm1.DBGrid1CellClick(Column: TColumn);begin//Making the drop-down pick list appear fasterif Column.PickList.Count > 0 thenbegin
keybd_event(VK_F2,0,0,0);
keybd_event(VK_F2,0,KEYEVENTF_KEYUP,0);
keybd_event(VK_MENU,0,0,0);
keybd_event(VK_DOWN,0,0,0);
keybd_event(VK_DOWN,0,KEYEVENTF_KEYUP,0);
keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0);end;end;

翻译自: https://www.thoughtco.com/drop-down-pick-list-into-dbgrid-4077749

dbgrid

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值