通过单击列标题对 DBGrid 进行排序
嗯,这似乎有点棘手(如果不是不可能的话)。 我试图通过单击列标题来让 DBGrid 对数据进行排序。
问题是我(遗憾的是)使用 Delphi 3,我没有使用 ADO DataSets 并且查询获取很多行,因此我无法重新打开我的 TQuery 更改 order by
关于点击的条款。
有人实施过类似的事情吗?
Well, this seems a little tricky (if not imposible). I'm trying to make my DBGrid sort its data by clicking on column's title.
The thing is that I'm (sadly) working with Delphi 3, I'm not using ADO DataSets and the query gets a lot of rows, thus I can't reopen my TQuery changing the order by
clause on clicks.
Someone has implemented something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这实际上是通过对数据集进行排序来完成的,然后网格反映变化。 通过在该列的数据集字段上创建索引就可以轻松完成此操作。 当然,这只能在支持索引排序的数据集上完成,例如
TClientDataset
。This is actually done by sorting the dataset, and then the grid reflects the change. It can be done easily enough by creating an index on the dataset field for that column. Of course, this can only be done on a dataset that supports index sorting, such as
TClientDataset
.在 TDBGrid 的 OnTitleClick 方法上,您可以执行类似的操作...
我确信有很多方法可以优化它,但这取决于您使用的组件的功能。 上面的示例使用查询组件,但如果您使用表组件,您将更改使用的索引而不是“order by”子句。
这里的 SQL 处理是一个非常基本的版本。 它不处理诸如 SQL 批处理语句之类的事情,从而可能导致多个“order by..”子句或注释 SQL 语句,即忽略括号注释“{..}”或单行注释“//”
问候
On the TDBGrid's OnTitleClick method you can do something like...
There are plenty of ways in which you could optimise this I'm sure however it depends on the capabilities of the components you use. The example above uses a query component though if you used a table component you'd change the index used instead of the 'order by' clause.
The handling of the SQL here is a very basic version. It does not handle things like SQL batch statements, resulting in possible multiple 'order by..' clauses or commented SQL statements i.e. ignoring bracketed comments "{..}" or single line comments "//"
Regards
如果您使用
TFDQuery
、TDataSource
和TDBGrid
的组合,您可以通过这种简单的方式进行订购!将其放入帮助程序文件中,以便稍后可以再次使用它。
If you are using a combination of
TFDQuery
,TDataSource
andTDBGrid
you can order with this easy way!Put it in a helper file so you can use it latter again.
Delphi 3 有
TClientDataset
。TQuery
可以使用数据库上显式创建的索引来对IndexName
属性上的数据进行排序。Delphi 3 have
TClientDataset
. AndTQuery
can use explicitly created indexes on the database to order data on theIndexName
property.以下是如何执行此操作的一些示例: 通过单击在 Delphi DBGrid 中对记录进行排序列标题。
正如已经提到的,如果您在
OnTitleClick
中使用TClientDataSet
(cds.IndexFieldNames := Column.FieldName
),排序就相当容易了。代码>TDBGrid)。 但是,如果您无法执行此操作,您可以重新生成查询(您已声明您不想这样做)或获取更高级的数据网格,例如 Express Quantum Grid (我认为它允许您排序)。Here are some examples of how to do this: Sorting records in Delphi DBGrid by Clicking on Column Title .
As already mentioned, sorting is quite easy if you are using a
TClientDataSet
(cds.IndexFieldNames := Column.FieldName
in theOnTitleClick
of theTDBGrid
). However if you are not able to do this you can either regenerate your query (which you have stated you don't want to do) or obtain a more advanced data grid such as Express Quantum Grid (which I think allows you to sort).在 TDBGrid 的 OnTitleClick 方法上,您可以编写以下简单的代码:
On the TDBGrid's OnTitleClick method you can write this simple code:
示例: (https://www.thoughtco.com/sort-records -in-delphi-dbgrid-4077301)
example: (https://www.thoughtco.com/sort-records-in-delphi-dbgrid-4077301)
升序和降序模式
Ascending and Descending Mode