delphi dbgrid_如何在Delphi DBGrid中对记录进行排序

delphi dbgrid

Delphi DBGrid is such a powerful component that you're probably using it every day if you're developing data-aware applications. Below, we'll take a look at how to add some more features to your database applications that your users are sure to love.

Delphi DBGrid是一个功能强大的组件,如果您正在开发数据感知应用程序,则可能每天都在使用它。 下面,我们将研究如何向您的用户肯定喜欢的数据库应用程序添加更多功能。

Following the concepts described in the Beginners Guide to Delphi Database Programming, the examples below use ADO components (AdoQuery/AdoTable connected to ADOConnection, DBGrid connected to AdoQuery over DataSource) to display the records from a database table in a DBGrid component.

遵循《 Delphi数据库编程初学者指南》中描述的概念,下面的示例使用ADO组件(连接到ADOConnection的AdoQuery / AdoTable,通过数据源连接到AdoQuery的DBGrid)来显示DBGrid组件中数据库表中的记录

All the component names were left as Delphi named them when dropped on the form (DBGrid1, ADOQuery1, AdoTable1, etc.).

当所有组件名称放在窗体上(DBGrid1,ADOQuery1,AdoTable1等)时,都以Delphi命名。

鼠标移到DBGrid标题区域上 ( Mouse Moves Over DBGrid Title Area )

First, let's see how to change the mouse pointer while it moves over the DBGrid title area. All you have to do is add the code to the OnMouseMove event for the DBGrid component.

首先,让我们看看如何在将鼠标指针移到DBGrid标题区域上时对其进行更改。 您所要做的就是将代码添加到DBGrid组件的OnMouseMove事件中。

The code below simply uses the MouseCoord property of the DBGrid component to "calculate" where the mouse pointer is. If it's over the DGBrid title area, the pt.y equals 0, which is the first row in the DBGrid (the title area displaying column/field titles).

下面的代码仅使用DBGrid组件的MouseCoord属性来“计算”鼠标指针所在的位置。 如果它在DGBrid标题区域上,则pt.y等于0,它是DBGrid中的第一行(标题区域显示列/字段标题)。


procedure TForm1.DBGrid1MouseMove
(Sender: TObject; Shift: TShiftState; X, Y: Integer);var
pt: TGridcoord;begin
pt:= DBGrid1.MouseCoord(x, y);if pt.y=0 then
DBGrid1.Cursor:=crHandPointelse
DBGrid1.Cursor:=crDefault;end;

按列排序并更改列标题字体 ( Sort on Column Click and Change the Column Title Font )

If you're using the ADO approach to Delphi database development, and want to sort the records in the dataset, you need to set the Sort property of your AdoDataset (ADOQuery, AdoTable).

如果您使用ADO方法进行Delphi数据库开发,并且想要对数据集中的记录进行排序,则需要设置AdoDataset的Sort属性(ADOQuery,AdoTable)。

The Sort property is the widestring value indicating the "ORDER BY" part of the standard SQL query. Of course, you do not need to write the SQL query to be able to use the Sort property. Simply set the Sort property to the name of a single field or to a comma-separated list of fields, each following the sort order.

Sort属性是一个宽字符串值,它指示标准SQL查询的“ ORDER BY”部分。 当然,您无需编写SQL查询即可使用Sort属性。 只需将“排序”属性设置为单个字段的名称或以逗号分隔的字段列表,每个字段均遵循排序顺序。

Here's an example:

这是一个例子:


ADOTable1.Sort := 'Year DESC, ArticleDate ASC'

The OnTitleClick event of the DBGrid component has a Column parameter indicating the Column the user has clicked on. Each Column (object of type TColumn) has a Field property indicating the Field (TField) represented by the Column, and the Field in its FieldName property holds the name of the field in the underlying dataset.

DBGrid组件的OnTitleClick事件具有Column参数,指示用户单击了Column。 每个Column(类型为TColumn的对象)都具有Field属性,该属性指示由Column表示的Field(TField),并且其FieldName属性中的Field保留基础数据集中的字段名称。

Therefore, to sort an ADO dataset by field/column, a simple line can be used:

因此,要按字段/列对ADO数据集进行排序,可以使用一条简单的行:


with TCustomADODataSet(DBGrid1.DataSource.DataSet) doSort := Column.Field.FieldName; // + ' ASC' or ' DESC'

Below is the code for the OnTitleClick even handler that sorts the records by column click. The code, as always, extends the idea.

下面是OnTitleClick偶数处理程序的代码,该代码按列单击对记录进行排序。 与往常一样,该代码扩展了这个想法。

First, we want to, in some way, mark the column that's currently used for sort order. Next, if we click on a column title and the dataset is already sorted by that column, we want to change the sort order from ASC (ascending) to DESC (descending), and vice versa. Finally, when we sort the dataset by another column, we want to remove the mark from the previously selected column.

首先,我们希望以某种方式标记当前用于排序顺序的列。 接下来,如果我们单击列标题,并且数据集已经按该列排序,我们想要将排序顺序从ASC(升序)更改为DESC(降序),反之亦然。 最后,当我们按另一列对数据集进行排序时,我们想从先前选择的列中删除标记。

For the sake of simplicity, to mark the column that "sorts" the records, we'll simply change the font style of the column title to Bold, and remove it when dataset is sorted using another column.

为了简单起见,为了标记对记录进行“排序”的列,我们将列标题的字体样式简单地更改为粗体,并在使用另一列对数据集进行排序时将其删除。


procedure TForm1.DBGrid1TitleClick(Column: TColumn);{$J+}const PreviousColumnIndex : integer = -1;{$J-}beginif DBGrid1.DataSource.DataSet is TCustomADODataSet thenwith TCustomADODataSet(DBGrid1.DataSource.DataSet) dobegintry
DBGrid1.Columns[PreviousColumnIndex].title.Font.Style :=
DBGrid1.Columns[PreviousColumnIndex].title.Font.Style - [fsBold];exceptend;
Column.title.Font.Style :=
Column.title.Font.Style + [fsBold];
PreviousColumnIndex := Column.Index;if (Pos(Column.Field.FieldName, Sort) = 1)and (Pos(' DESC', Sort)= 0) then
Sort := Column.Field.FieldName + ' DESC'else
Sort := Column.Field.FieldName + ' ASC';end;end;

The above code uses typed constants to preserve the value of the previously "selected" column for sort order.

上面的代码使用类型化的常数来保留先前“选定”列的值以进行排序。

翻译自: https://www.thoughtco.com/sort-records-in-delphi-dbgrid-4077301

delphi dbgrid

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值