delphi dbgrid
Delphi's DBGrid is one of the most widely used DB-aware components in database related applications. Its main purpose is to enable your application's users to manipulate records from a dataset in a tabular grid.
Delphi的 DBGrid是数据库相关应用程序中使用最广泛的DB感知组件之一。 其主要目的是使应用程序的用户能够处理表格网格中数据集的记录。
One of the lesser known features of the DBGrid component is that it can be set to allow multiple row selection. What this means is that your users can have the ability to select multiple records (rows) from the dataset connected to the grid.
DBGrid组件鲜为人知的功能之一是可以将其设置为允许多行选择。 这意味着您的用户可以从连接到网格的数据集中选择多个记录(行)。
允许多项选择 ( Allowing Multiple Selections )
To enable multiple selection, you only need to set the dgMultiSelect element to "True" in the Options property. When dgMultiSelect is "True," users can select multiple rows in a grid using the following techniques:
要启用多个选择,只需在Options属性中将dgMultiSelect元素设置为“ True”。 当dgMultiSelect为“ True”时,用户可以使用以下技术选择网格中的多行 :
Ctrl + Mouse click
Ctrl +鼠标单击
Shift + Arrow keys
Shift +方向键
The selected rows/records are represented as bookmarks and stored in the grid's SelectedRows property.
选定的行/记录以书签表示,并存储在网格的SelectedRows属性中。
Note that SelectedRows is only useful when the Options property is set to "True" for both dgMultiSelect and dgRowSelect. On the other hand, when using dgRowSelect (when individual cells cannot be selected) the user won't be able to edit records directly through the grid and, and dgEditing is automatically set to "False."
请注意,仅当dgMultiSelect和dgRowSelect的Options属性都设置为“ True”时, SelectedRows才有用。 另一方面,使用dgRowSelect时 (无法选择单个单元格时),用户将无法直接通过网格来编辑记录,并且dgEditing会自动设置为“ False”。
The SelectedRows property is an object of type TBookmarkList. We can use the SelectedRows property to, for example:
SelectedRows属性是TBookmarkList类型的对象。 我们可以使用SelectedRows属性来例如:
- Get the number of rows selected 获取所选的行数
- Clear the selection (unselect) 清除选择(取消选择)
- Delete all the selected records 删除所有选定的记录
- Check whether a particular record is selected 检查是否选择了特定记录
To set dgMultiSelect to "True," you can either use the Object Inspector at design time or use a command like this at runtime:
要将dgMultiSelect设置为“ True”,可以在设计时使用对象检查器 ,也可以在运行时使用如下命令:
DBGrid1.Options:= DBGrid1.Options + [dgMultiSelect];
dgMultiSelect示例 ( dgMultiSelect Example )
A good situation in which to use dgMultiSelect might be when you need an option to select random records or if you need the sum of the values of the selected fields.
使用dgMultiSelect的一种好情况是,当您需要选择随机记录的选项,或者需要所选字段的值的总和时。
The example below uses ADO components (AdoQuery connected to ADOConnection and DBGrid connected to AdoQuery over DataSource) to display the records from a database table in a DBGrid component.
下面使用的示例ADO组件( AdoQuery连接到的ADOConnection和DBGrid的连接到超过AdoQuery 数据源 )的一个DBGrid组件来显示从数据库表中的记录。
The code uses multiple selection to get the sum of the values in the "Size" field. Use this sample code if you want to select the entire DBGrid:
该代码使用多项选择来获取“大小”字段中值的总和。 如果要选择整个DBGrid,请使用以下示例代码:
procedure TForm1.btnDoSumClick(Sender: TObject);var
i: Integer;
sum : Single;beginif DBGrid1.SelectedRows.Count > 0 thenbegin
sum := 0;with DBGrid1.DataSource.DataSet dobeginfor i := 0 to DBGrid1.SelectedRows.Count-1 dobegin
GotoBookmark(Pointer(DBGrid1.SelectedRows.Items[i]));
sum:= sum + AdoQuery1.FieldByName('Size').AsFloat;end;end;
edSizeSum.Text := FloatToStr(sum);endend;
翻译自: https://www.thoughtco.com/multiselect-in-the-delphi-dbgrid-4077282
delphi dbgrid