通过单击列标题对 DBGrid 进行排序

发布于 2024-07-27 18:44:13 字数 195 浏览 9 评论 0原文

嗯,这似乎有点棘手(如果不是不可能的话)。 我试图通过单击列标题来让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

绝對不後悔。 2024-08-03 18:44:13

这实际上是通过对数据集进行排序来完成的,然后网格反映变化。 通过在该列的数据集字段上创建索引就可以轻松完成此操作。 当然,这只能在支持索引排序的数据集上完成,例如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.

oО清风挽发oО 2024-08-03 18:44:13

在 TDBGrid 的 OnTitleClick 方法上,您可以执行类似的操作...

procedure TfmForm1.DBGrid1TitleClick(Column: TColumn);
var
   i: Integer;
begin
   // apply grid formatting changes here e.g. title styling
   with DBGrid1 do
      for i := 0 to Columns.Count - 1 do
         Columns[i].Title.Font.Style := Columns[i].Title.Font.Style - [fsBold];
   Column.Title.Font.Style := Column.Title.Font.Style + [fsBold];

   with nxQuery1 do // the DBGrid's query component (a [NexusDB] TnxQuery)
   begin
      DisableControls;
      if Active then Close;
      for i := 0 to SQL.Count - 1 do
         if (Pos('order by', LowerCase(SQL[i])) > 0) then
            //NOTE: ' desc' The [space] is important
            if (Pos(' desc',LowerCase(SQL[i])) > 0) then 
               SQL[i] := newOrderBySQL
            else
               SQL[i] := newOrderBySQL +' desc';
      // re-add params here if necessary
      if not Active then Open;
      EnableControls;
   end;
end;

我确信有很多方法可以优化它,但这取决于您使用的组件的功能。 上面的示例使用查询组件,但如果您使用表组件,您将更改使用的索引而不是“order by”子句。

这里的 SQL 处理是一个非常基本的版本。 它不处理诸如 SQL 批处理语句之类的事情,从而可能导致多个“order by..”子句或注释 SQL 语句,即忽略括号注释“{..}”或单行注释“//”

问候

On the TDBGrid's OnTitleClick method you can do something like...

procedure TfmForm1.DBGrid1TitleClick(Column: TColumn);
var
   i: Integer;
begin
   // apply grid formatting changes here e.g. title styling
   with DBGrid1 do
      for i := 0 to Columns.Count - 1 do
         Columns[i].Title.Font.Style := Columns[i].Title.Font.Style - [fsBold];
   Column.Title.Font.Style := Column.Title.Font.Style + [fsBold];

   with nxQuery1 do // the DBGrid's query component (a [NexusDB] TnxQuery)
   begin
      DisableControls;
      if Active then Close;
      for i := 0 to SQL.Count - 1 do
         if (Pos('order by', LowerCase(SQL[i])) > 0) then
            //NOTE: ' desc' The [space] is important
            if (Pos(' desc',LowerCase(SQL[i])) > 0) then 
               SQL[i] := newOrderBySQL
            else
               SQL[i] := newOrderBySQL +' desc';
      // re-add params here if necessary
      if not Active then Open;
      EnableControls;
   end;
end;

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

冰雪之触 2024-08-03 18:44:13

如果您使用 TFDQueryTDataSourceTDBGrid 的组合,您可以通过这种简单的方式进行订购!

procedure TFrmGer.DBGridTitleClick(Column: TColumn);
begin
  OrderByTitle(MyFDQuery, Column);
end;

将其放入帮助程序文件中,以便稍后可以再次使用它。

procedure OrderByTitle(AQuery: TFDQuery; Column: TColumn);
begin
  AQuery.IndexFieldNames := Column.DisplayName;
end;

If you are using a combination of TFDQuery, TDataSource and TDBGrid you can order with this easy way!

procedure TFrmGer.DBGridTitleClick(Column: TColumn);
begin
  OrderByTitle(MyFDQuery, Column);
end;

Put it in a helper file so you can use it latter again.

procedure OrderByTitle(AQuery: TFDQuery; Column: TColumn);
begin
  AQuery.IndexFieldNames := Column.DisplayName;
end;
空城缀染半城烟沙 2024-08-03 18:44:13

Delphi 3 有TClientDatasetTQuery 可以使用数据库上显式创建的索引来对 IndexName 属性上的数据进行排序。

Delphi 3 have TClientDataset. And TQuery can use explicitly created indexes on the database to order data on the IndexName property.

叫嚣ゝ 2024-08-03 18:44:13

以下是如何执行此操作的一些示例: 通过单击在 Delphi DBGrid 中对记录进行排序列标题

正如已经提到的,如果您在 OnTitleClick 中使用 TClientDataSetcds.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 the OnTitleClick of the TDBGrid). 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).

梦醒灬来后我 2024-08-03 18:44:13

在 TDBGrid 的 OnTitleClick 方法上,您可以编写以下简单的代码:

procedure TForm1.DBGrid3TitleClick(Column: TColumn);
var
  cFieldName:string;
begin
  cFieldName:= DBGrid3.SelectedField.FieldName;
  AdoDataset1.Sort:=cFieldName;
end;

On the TDBGrid's OnTitleClick method you can write this simple code:

procedure TForm1.DBGrid3TitleClick(Column: TColumn);
var
  cFieldName:string;
begin
  cFieldName:= DBGrid3.SelectedField.FieldName;
  AdoDataset1.Sort:=cFieldName;
end;
梦罢 2024-08-03 18:44:13

示例: (https://www.thoughtco.com/sort-records -in-delphi-dbgrid-4077301)

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:=crHandPoint
 else
 DBGrid1.Cursor:=crDefault;
end;

procedure TForm1.DBGrid1TitleClick(Column: TColumn);
var
  cFieldName:string;
begin
  Adotable1.Sort := Column.Field.FieldName;
end;

example: (https://www.thoughtco.com/sort-records-in-delphi-dbgrid-4077301)

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:=crHandPoint
 else
 DBGrid1.Cursor:=crDefault;
end;

procedure TForm1.DBGrid1TitleClick(Column: TColumn);
var
  cFieldName:string;
begin
  Adotable1.Sort := Column.Field.FieldName;
end;
与君绝 2024-08-03 18:44:13

升序和降序模式

  if Pos('DESC',PChar(Q2.Sort))>0 then
    Q2.Sort:=Column.FieldName  + ' ASC'
  else
    Q2.Sort:=Column.FieldName  + ' DESC';

Ascending and Descending Mode

  if Pos('DESC',PChar(Q2.Sort))>0 then
    Q2.Sort:=Column.FieldName  + ' ASC'
  else
    Q2.Sort:=Column.FieldName  + ' DESC';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文