Delphi DBGrid 数据排序(ADOQuery、ADOTable、AdoDataSet、Clientdataset、UniQuery、FDQuery)

Delphi DBGrid 数据排序(ADOQuery、ADOTable、AdoDataSet、Clientdataset、UniQuery、FDQuery)

1、DBGrid 配合ADOQuery 使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
var
  i: integer;
begin
  for i := 1 to DBGrid1.Columns.Count do
  begin
//恢复所有标题字体为默认
    DBGrid1.Columns[i - 1].Title.Font.Color := clWindowText;
    DBGrid1.Columns[i - 1].Title.Font.Style := [];
  end;
  if ADOQuery1.Sort <> (Column.FieldName + ' ASC') then //判断原排序方式
  begin
    ADOQuery1.Sort := Column.FieldName + ' ASC';
    Column.Title.Font.Color := clRed; //改变标题行字体为红色,表示当前的排序方式为升序
    Column.Title.Font.Style := [fsBold];
  end
  else
  begin
    ADOQuery1.Sort := Column.FieldName + ' DESC';
    Column.Title.Font.Color := clBlue; //改变标题行字体为红色,表示当前的排序方式为降序
    Column.Title.Font.Style := [fsBold];
  end;
end;

2、DBGrid 配合ADOTable 操作类似

1
2
3
4
5
6
7
8
9
10
11
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
begin
  with ADOTable1 do
  begin
    if DBGrid1Boolean then
      TADOTable(ryADOTable1).Sort := Column.FieldName + ' DESC'
    else
      TADOTable(ryADOTable1).Sort := Column.FieldName;
    DBGrid1Boolean := not (DBGrid1Boolean);
  end;
end;

3、其他参考(AdoDataSet、Clientdataset)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//How to Use:
//procedure TForm1.DBGrid1TitleClick(Column: TColumn);
//begin
// GridTitleSort(column);
//end;
 
procedure GridTitleSort(Column: TColumn);
type
  TFieldTypeSet = set of TFieldType;
var
  s, cFieldName: string;
  i: integer;
  DataSet: TDataSet;
  GridFieldTypeSet: TFieldTypeSet;
 
  procedure SetTitle;
  var
    ii: integer;
    cStr: string;
    c: TColumn;
  begin
    for ii := 0 to TDBGrid(Column.Grid).Columns.Count - 1 do
    begin
      c := TDBGrid(Column.Grid).Columns[ii];
      cStr := c.Title.Caption;
      if (pos('↑', cStr) = 1) or (pos('↓', cStr) = 1) then
      begin
        Delete(cStr, 1, 2);
        c.Title.Caption := cStr;
      end;
    end;
  end;
 
begin
  DataSet := Column.Grid.DataSource.DataSet;
 
  GridFieldTypeSet := [ftString, ftSmallint, ftInteger, ftWord, ftBoolean, ftFloat, ftCurrency, ftBCD, ftDate, ftTime, ftDateTime, ftBytes, ftVarBytes, ftTypedBinary, ftFixedChar, ftWideString, ftLargeint, ftVariant];
  if not (Column.Field.DataType in GridFieldTypeSet) then
    Exit; //§P&Acirc;_&brvbar;r&not;q&Atilde;&thorn;&laquo;&not;
 
  SetTitle;
  if Column.Field.FieldKind = fkLookup then
    cFieldName := Column.Field.KeyFields
  else if Column.Field.FieldKind = fkCalculated then
    cFieldName := Column.Field.KeyFields
  else
    cFieldName := Column.FieldName;
//=================================AdoDataSet=====================
  if DataSet is TCustomADODataSet then
  begin
    s := TCustomADODataSet(DataSet).Sort;
    if s = '' then
    begin
      s := cFieldName;
      Column.Title.Caption := '↑' + Column.Title.Caption;
    end
    else
    begin
      if Pos(cFieldName, s) <> 0 then
      begin
        i := Pos('DESC', s);
        if i <= 0 then
        begin
          s := s + ' DESC';
          Column.Title.Caption := '↓' + Column.Title.Caption;
        end
        else
        begin
          Column.Title.Caption := '↑' + Column.Title.Caption;
          Delete(s, i, 4);
        end;
      end
      else
      begin
        s := cFieldName;
        Column.Title.Caption := '↑' + Column.Title.Caption;
      end;
    end;
    TCustomADODataSet(DataSet).Sort := s;
  end
//============================Clientdataset==========================
  else if DataSet is TClientDataSet then
  begin
    if TClientDataSet(DataSet).indexfieldnames <> '' then
    begin
      i := TClientDataSet(DataSet).IndexDefs.IndexOf('i' + Column.FieldName);
      if i = -1 then
      begin
        with TClientDataSet(DataSet).IndexDefs.AddIndexDef do
        begin
          Name := 'i' + Column.FieldName;
          Fields := Column.FieldName;
          DescFields := Column.FieldName;
        end;
      end;
      TClientDataSet(DataSet).IndexFieldNames := '';
      TClientDataSet(DataSet).IndexName := 'i' + Column.FieldName;
      Column.Title.Caption := '↓' + Column.Title.Caption;
    end
    else
    begin
      TClientDataSet(DataSet).IndexName := '';
      TClientDataSet(DataSet).IndexFieldNames := column.fieldname;
      Column.Title.Caption := '↑' + Column.Title.Caption;
    end;
  end;
end;

4、如果使用的是 UniQuery 操作类似:

操作 UniQuery 的 IndexFieldNames属性:

1
indexfieldnames:='字段 desc',

5、FDQuery  

   参考操作 4

 

 

创建时间:2020.09.11  更新时间:2021.01.22 /  2021.06.16

 

posted on   滔Roy  阅读(2822)  评论(4编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2019-09-11 锐浪报表 Grid++Report 一维码无法固定条形码打印宽度
2019-09-11 Delphi中点击网页弹出的Alert对话框的确定按钮
2019-09-11 Delphi WinAPI FindWindow、FindWindowEx、EnumChildWindows、SendMessage - 实现获取句柄并发送消息的方法
2019-09-11 Delphi 常用API 函数列表
2019-09-11 Delphi WinAPI 消息函数 SendMessage函数和 PostMessage的区别
2019-09-11 Delphi Win API 消息函数 SendMessage函数
2019-09-11 Delphi Close、Halt、terminate、ExitProcess的区别

导航

点击右上角即可分享
微信分享提示