请问在DBGRID中,如何单击标题进行排序?(50分)

  • 主题发起人 主题发起人 stonelei
  • 开始时间 开始时间
S

stonelei

Unregistered / Unconfirmed
GUEST, unregistred user!
请问在DBGRID中单击那个标题就按那个标题的字段进行排序?
击一下是从大到小,再击一下就从小到大?
谢谢.
 
换控件DXDBGRID
 
事件可以在ontitleclick中捕捉
但排序要自己写代码完成的。
 
份太少了
 
procedure TForm.DBGrid1TitleClick(Column: TColumn);
var
indexfield : string;
begin
with DataModule1.DataAdoQ do begin
indexField := column.Field.FieldName;
close;
sql.Clear;
sql.add('Select * From table');
sql.add(' order by ');
sql.add(indexField);
open;
end;
end;
 
procedure Tfrm_xmwjcx.DBGrid1TitleClick(Column: TColumn);

begin
query1.Close ;
if query1.SQL.Count <2 then
begin
case column.Index of
0: query1.sql.add('order by wjbh');
1: query1.sql.add('order by wjbt');
2: query1.sql.add('order by wjnb');
3: query1.sql.add('order by fwjg');
4: query1.sql.add('order by fwrq');
end;
end
else
begin
case column.Index of
0: query1.sql[1]:='order by wjbh';
1: query1.sql[1]:='order by wjbt';
2: query1.sql[1]:='order by wjnb';
3: query1.sql[1]:='order by fwjg';
4: query1.sql[1]:='order by fwrq';
end;
end;
query1.Open;
end;
 
从公司的代码中复制一段给你(这段代码也是我写的),
当然可以使用了!不过分数一定要全部给我才对呀!!!
(支持TLookUp类型的字段!!!支持重复点击!!!)

procedure TfrmQueryBaseCls.dgridMainTitleClick(Column: TColumn);
begin
SortFieldForTitleClick((Column.Field.DataSet as TClientDataSet), Column);
end;

procedure SortFieldForTitleClick(ADataSet: TClientDataSet; AColumn: TColumn);
var //排序鼠标点击的列↓↑
sIndexName, sDispFieldName, sIndexFieldName, sTitle: string;
vDatas: OleVariant;
tmpDef: TIndexDef;
I: Integer;
begin
sDispFieldName := AColumn.FieldName;
sIndexFieldName := AColumn.FieldName;
if (ADataSet.FindField(sDispFieldName).FieldKind <> fkData) and (ADataSet.FindField(sDispFieldName).FieldKind <> fkLookUp) then
Exit;
if (ADataSet.FindField(sDispFieldName).FieldKind = fkLookUp) then
sIndexFieldName := ADataSet.FindField(sDispFieldName).KeyFields;

for I := 0 to ADataSet.FieldCount - 1 do
begin
ADataSet.Fields.DisplayLabel := StringReplace(ADataSet.Fields.DisplayLabel, '↑', '', [rfReplaceAll, rfIgnoreCase]);
ADataSet.Fields.DisplayLabel := StringReplace(ADataSet.Fields.DisplayLabel, '↓', '', [rfReplaceAll, rfIgnoreCase]);
end;

sTitle := ADataSet.FieldByName(sDispFieldName).DisplayLabel;
sIndexName := 'Index' + sIndexFieldName;
vDatas := ADataSet.Data;

if ADataSet.IndexDefs.IndexOf(sIndexName) < 0 then
begin
with ADataSet.IndexDefs do begin
with AddIndexDef do begin
Name := sIndexName;
Fields := sIndexFieldName;
end;
end;
end;

tmpDef := ADataSet.IndexDefs.Find(sIndexName);
if tmpDef <> nil then
with tmpDef do
begin
if DescFields = '' then
begin
DescFields := sIndexFieldName;
Options := Options + [ixDescending];
sTitle := sTitle + '↑';
end else
begin
DescFields := '';
Options := Options - [ixDescending];
sTitle := sTitle + '↓';
end;
end;
ADataSet.Data := vDatas;
ADataSet.FieldByName(sDispFieldName).DisplayLabel := sTitle;
ADataSet.Open;
ADataSet.IndexName := sIndexName;
end;
 
可以做到,就像最初 wuchi 讲的,
但我强烈劝你用dxDBGrid,只要设置两三个属性就可以做到这一点,
而且还有很多强大实用的功能,虽然它也还有一些bug,用了,你会喜欢它的
 
你自己修改一下吧

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, Grids, DBGrids;

type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DataSource1: TDataSource;
Table1: TTable;
procedure DBGrid1DblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
SortBool: Boolean = True;
implementation

{$R *.dfm}

procedure TForm1.DBGrid1DblClick(Sender: TObject);
var
i: Integer;
begin
Screen.Cursor := crHourGlass;
for i := 0 to (Sender as TDBGrid).Columns.Count - 1 do
if (Copy((Sender as TDBGrid).Columns.FieldName, 1, 3) = '▲ ') or (Copy((Sender as TDBGrid).Columns.FieldName, 1, 3) = '▼ ') then
(Sender as TDBGrid).Columns.FieldName := Copy((Sender as TDBGrid).Columns.FieldName, 4, Length((Sender as TDBGrid).Columns.FieldName) - 3);
case (Sender as TDBGrid).Columns.Index of
0,1,2,3,4,5,6,7,11,12,13,14,20,21,23,24,25,26,27,28,30,31:
if SortBool then
begin
if (Sender as TDBGrid).SelectedRows <> nil then
begin
QuickSortStringAscend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, True);
SortBool := not SortBool;
end
else
begin
QuickSortStringAscend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, False);
SortBool := not SortBool;
end;
(Sender as TListView).Columns[Column.Index].Caption := '▲ ' + (Sender as TListView).Columns[Column.Index].Caption;
end
else
begin
if (Sender as TListView).Selected <> nil then
begin
QuickSortStringDescend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, True);
SortBool := not SortBool;
end
else
begin
QuickSortStringDescend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, False);
SortBool := not SortBool;
end;
(Sender as TListView).Columns[Column.Index].Caption := '▼ ' + (Sender as TListView).Columns[Column.Index].Caption;
end;
9,10,15,22:
if SortBool then
begin
if (Sender as TListView).Selected <> nil then
begin
QuickSortInt64Ascend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, True);
SortBool := not SortBool;
end
else
begin
QuickSortInt64Ascend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, False);
SortBool := not SortBool;
end;
(Sender as TListView).Columns[Column.Index].Caption := '▲ ' + (Sender as TListView).Columns[Column.Index].Caption;
end
else
begin
if (Sender as TListView).Selected <> nil then
begin
QuickSortInt64Descend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, True);
SortBool := not SortBool;
end
else
begin
QuickSortInt64Descend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, False);
SortBool := not SortBool;
end;
(Sender as TListView).Columns[Column.Index].Caption := '▼ ' + (Sender as TListView).Columns[Column.Index].Caption;
end;
8,16,29:
if SortBool then
begin
if (Sender as TListView).Selected <> nil then
begin
QuickSortExtendedAscend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, True);
SortBool := not SortBool;
end
else
begin
QuickSortExtendedAscend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, False);
SortBool := not SortBool;
end;
(Sender as TListView).Columns[Column.Index].Caption := '▲ ' + (Sender as TListView).Columns[Column.Index].Caption;
end
else
begin
if (Sender as TListView).Selected <> nil then
begin
QuickSortExtendedDescend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, True);
SortBool := not SortBool;
end
else
begin
QuickSortExtendedDescend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, False);
SortBool := not SortBool;
end;
(Sender as TListView).Columns[Column.Index].Caption := '▼ ' + (Sender as TListView).Columns[Column.Index].Caption;
end;
17,18,19:
if SortBool then
begin
if (Sender as TListView).Selected <> nil then
begin
QuickSortDateTimeAscend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, True);
SortBool := not SortBool;
end
else
begin
QuickSortDateTimeAscend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, False);
SortBool := not SortBool;
end;
(Sender as TListView).Columns[Column.Index].Caption := '▲ ' + (Sender as TListView).Columns[Column.Index].Caption;
end
else
begin
if (Sender as TListView).Selected <> nil then
begin
QuickSortDateTimeDescend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, True);
SortBool := not SortBool;
end
else
begin
QuickSortDateTimeDescend((Sender as TListView), Column.Index, 0, (Sender as TListView).Items.Count -1, False);
SortBool := not SortBool;
end;
(Sender as TListView).Columns[Column.Index].Caption := '▼ ' + (Sender as TListView).Columns[Column.Index].Caption;
end;
end;
Screen.Cursor := crDefault;
end;

end.


procedure ListItemExchange(ListItemA, ListItemB: TListItem; Count: Integer);
var
Temp: String;
i: Integer;
begin
for i := 0 to Count - 1 do
begin
if i = 0 then
begin
Temp := ListItemA.Caption;
ListItemA.Caption := ListItemB.Caption;
ListItemB.Caption := Temp;
end
else
begin
Temp := ListItemA.SubItems[i - 1];
ListItemA.SubItems[i - 1] := ListItemB.SubItems[i - 1];
ListItemB.SubItems[i - 1] := Temp;
end;
end;
end;

Procedure QuickSortStringAscend(ListView: TListView; Column, iLo, iHi: Integer; Selected: Boolean);
var
Lo, Hi: Integer;
Mid: String;
begin
if Column = 0 then
begin
Lo := iLo;
Hi := iHi;
Mid := ListView.Items.Item[(Lo + Hi) div 2].Caption;
repeat
while CompareText(ListView.Items.Item[Lo].Caption, Mid) < 0 do Inc(Lo);
while CompareText(ListView.Items.Item[Hi].Caption, Mid) > 0 do Dec(Hi);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSortStringAscend(ListView, Column, iLo, Hi, Selected);
if Lo < iHi then QuickSortStringAscend(ListView, Column, Lo, iHi, Selected);
end
else
begin
Lo := iLo;
Hi := iHi;
Mid := ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1];
repeat
while CompareText(ListView.Items.Item[Lo].SubItems[Column - 1], Mid) < 0 do Inc(Lo);
while CompareText(ListView.Items.Item[Hi].SubItems[Column - 1], Mid) > 0 do Dec(Hi);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSortStringAscend(ListView, Column, iLo, Hi, Selected);
if Lo < iHi then QuickSortStringAscend(ListView, Column, Lo, iHi, Selected);
end;
end;

Procedure QuickSortStringDescend(ListView: TListView; Column, iLo, iHi: Integer; Selected: Boolean);
var
Lo, Hi: Integer;
Mid: String;
begin
if Column = 0 then
begin
Lo := iLo;
Hi := iHi;
Mid := ListView.Items.Item[(Lo + Hi) div 2].Caption;
repeat
while CompareText(ListView.Items.Item[Lo].Caption, Mid) > 0 do Inc(Lo);
while CompareText(ListView.Items.Item[Hi].Caption, Mid) < 0 do Dec(Hi);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi < iLo then QuickSortStringAscend(ListView, Column, iLo, Hi, Selected);
if Lo > iHi then QuickSortStringAscend(ListView, Column, Lo, iHi, Selected);
end
else
begin
Lo := iLo;
Hi := iHi;
Mid := ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1];
repeat
while CompareText(ListView.Items.Item[Lo].SubItems[Column - 1], Mid) > 0 do Inc(Lo);
while CompareText(ListView.Items.Item[Hi].SubItems[Column - 1], Mid) < 0 do Dec(Hi);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi < iLo then QuickSortStringAscend(ListView, Column, iLo, Hi, Selected);
if Lo > iHi then QuickSortStringAscend(ListView, Column, Lo, iHi, Selected);
end;
end;

Procedure QuickSortInt64Ascend(ListView: TListView; Column, iLo, iHi: Integer; Selected: Boolean);
var
Lo, Hi: Integer;
Mid: Int64;
begin
if Column = 0 then
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].Caption <> '' then
Mid := StrToInt64(ListView.Items.Item[(Lo + Hi) div 2].Caption)
else
Mid := MinInt64;
repeat
if ListView.Items.Item[Lo].Caption <> '' then
begin
while CompareValue(StrToInt64(ListView.Items.Item[Lo].Caption), Mid) < 0 do Inc(Lo);
while CompareValue(StrToInt64(ListView.Items.Item[Hi].Caption), Mid) > 0 do Dec(Hi);
end
else
Inc(Lo);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSortInt64Ascend(ListView, Column, iLo, Hi, Selected);
if Lo < iHi then QuickSortInt64Ascend(ListView, Column, Lo, iHi, Selected);
end
else
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1] <> '' then
Mid := StrToInt64(ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1])
else
Mid := MinInt64;
repeat
if ListView.Items.Item[Lo].SubItems[Column - 1] <> '' then
begin
while CompareValue(StrToInt64(ListView.Items.Item[Lo].SubItems[Column - 1]), Mid) < 0 do Inc(Lo);
while CompareValue(StrToInt64(ListView.Items.Item[Hi].SubItems[Column - 1]), Mid) > 0 do Dec(Hi);
end
else
Inc(Lo);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSortInt64Ascend(ListView, Column, iLo, Hi, Selected);
if Lo < iHi then QuickSortInt64Ascend(ListView, Column, Lo, iHi, Selected);
end;
end;

Procedure QuickSortInt64Descend(ListView: TListView; Column, iLo, iHi: Integer; Selected: Boolean);
var
Lo, Hi: Integer;
Mid: Int64;
begin
if Column = 0 then
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].Caption <> '' then
Mid := StrToInt64(ListView.Items.Item[(Lo + Hi) div 2].Caption)
else
Mid := MinInt64;
repeat
if ListView.Items.Item[Lo].Caption <> '' then
begin
while CompareValue(StrToInt64(ListView.Items.Item[Lo].Caption), Mid) > 0 do Inc(Lo);
while CompareValue(StrToInt64(ListView.Items.Item[Hi].Caption), Mid) < 0 do Dec(Hi);
end
else
Dec(Hi);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi < iLo then QuickSortInt64Ascend(ListView, Column, iLo, Hi, Selected);
if Lo > iHi then QuickSortInt64Ascend(ListView, Column, Lo, iHi, Selected);
end
else
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1] <> '' then
Mid := StrToInt64(ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1])
else
Mid := MinInt64;
repeat
if ListView.Items.Item[Lo].SubItems[Column - 1] <> '' then
begin
while CompareValue(StrToInt64(ListView.Items.Item[Lo].SubItems[Column - 1]), Mid) > 0 do Inc(Lo);
while CompareValue(StrToInt64(ListView.Items.Item[Hi].SubItems[Column - 1]), Mid) < 0 do Dec(Hi);
end
else
Dec(Hi);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi < iLo then QuickSortInt64Ascend(ListView, Column, iLo, Hi, Selected);
if Lo > iHi then QuickSortInt64Ascend(ListView, Column, Lo, iHi, Selected);
end;
end;

Procedure QuickSortExtendedAscend(ListView: TListView; Column, iLo, iHi: Integer; Selected: Boolean);
var
Lo, Hi: Integer;
Mid: Extended;
begin
if Column = 0 then
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].Caption = '' then
Mid := StrToFloat(ListView.Items.Item[(Lo + Hi) div 2].Caption)
else
Mid := NegativeMaxExtended;
repeat
if ListView.Items.Item[(Lo + Hi) div 2].Caption <> '' then
begin
while CompareValue(StrToFloat(ListView.Items.Item[Lo].Caption), Mid) < 0 do Inc(Lo);
while CompareValue(StrToFloat(ListView.Items.Item[Hi].Caption), Mid) > 0 do Dec(Hi);
end
else
Inc(Lo);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSortExtendedAscend(ListView, Column, iLo, Hi, Selected);
if Lo < iHi then QuickSortExtendedAscend(ListView, Column, Lo, iHi, Selected);
end
else
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1] <> '' then
Mid := StrToFloat(ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1])
else
Mid := NegativeMaxExtended;
repeat
if ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1] <> '' then
begin
while CompareValue(StrToFloat(ListView.Items.Item[Lo].SubItems[Column - 1]), Mid) < 0 do Inc(Lo);
while CompareValue(StrToFloat(ListView.Items.Item[Hi].SubItems[Column - 1]), Mid) > 0 do Dec(Hi);
end
else
Inc(Lo);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSortExtendedAscend(ListView, Column, iLo, Hi, Selected);
if Lo < iHi then QuickSortExtendedAscend(ListView, Column, Lo, iHi, Selected);
end;
end;

Procedure QuickSortExtendedDescend(ListView: TListView; Column, iLo, iHi: Integer; Selected: Boolean);
var
Lo, Hi: Integer;
Mid: Extended;
begin
if Column = 0 then
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].Caption <> '' then
Mid := StrToFloat(ListView.Items.Item[(Lo + Hi) div 2].Caption)
else
Mid := NegativeMaxExtended;
repeat
if ListView.Items.Item[(Lo + Hi) div 2].Caption <> '' then
begin
while CompareValue(StrToFloat(ListView.Items.Item[Lo].Caption), Mid) > 0 do Inc(Lo);
while CompareValue(StrToFloat(ListView.Items.Item[Hi].Caption), Mid) < 0 do Dec(Hi);
end
else
Dec(Hi);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi < iLo then QuickSortExtendedAscend(ListView, Column, iLo, Hi, Selected);
if Lo > iHi then QuickSortExtendedAscend(ListView, Column, Lo, iHi, Selected);
end
else
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1] <> '' then
Mid := StrToFloat(ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1])
else
Mid := NegativeMaxExtended;
repeat
if ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1] <> '' then
begin
while CompareValue(StrToFloat(ListView.Items.Item[Lo].SubItems[Column - 1]), Mid) > 0 do Inc(Lo);
while CompareValue(StrToFloat(ListView.Items.Item[Hi].SubItems[Column - 1]), Mid) < 0 do Dec(Hi);
end
else
Dec(Hi);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi < iLo then QuickSortExtendedAscend(ListView, Column, iLo, Hi, Selected);
if Lo > iHi then QuickSortExtendedAscend(ListView, Column, Lo, iHi, Selected);
end;
end;

procedure QuickSortDateTimeAscend(ListView: TListView; Column, iLo, iHi: Integer; Selected: Boolean);
var
Lo, Hi: Integer;
Mid: TDateTime;
begin
if Column = 0 then
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].Caption <> '' then
Mid := StrToDateTime(ListView.Items.Item[(Lo + Hi) div 2].Caption)
else
Mid := MinDateTime;
repeat
if ListView.Items.Item[(Lo + Hi) div 2].Caption <> '' then
begin
while CompareDateTime(StrToDateTime(ListView.Items.Item[Lo].Caption), Mid) < 0 do Inc(Lo);
while CompareDateTime(StrToDateTime(ListView.Items.Item[Hi].Caption), Mid) > 0 do Dec(Hi);
end
else
Inc(Lo);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSortDateTimeAscend(ListView, Column, iLo, Hi, Selected);
if Lo < iHi then QuickSortDateTimeAscend(ListView, Column, Lo, iHi, Selected);
end
else
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1] <> '' then
Mid := StrToDateTime(ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1])
else
Mid := MinDateTime;
repeat
if ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1] <> '' then
begin
while CompareDateTime(StrToDateTime(ListView.Items.Item[Lo].SubItems[Column - 1]), Mid) < 0 do Inc(Lo);
while CompareDateTime(StrToDateTime(ListView.Items.Item[Hi].SubItems[Column - 1]), Mid) > 0 do Dec(Hi);
end
else
Inc(Lo);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSortDateTimeAscend(ListView, Column, iLo, Hi, Selected);
if Lo < iHi then QuickSortDateTimeAscend(ListView, Column, Lo, iHi, Selected);
end;
end;

Procedure QuickSortDateTimeDescend(ListView: TListView; Column, iLo, iHi: Integer; Selected: Boolean);
var
Lo, Hi : Integer;
Mid: TDateTime;
begin
if Column = 0 then
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].Caption <> '' then
Mid := StrToDateTime(ListView.Items.Item[(Lo + Hi) div 2].Caption)
else
Mid := MinDateTime;
repeat
if ListView.Items.Item[(Lo + Hi) div 2].Caption <> '' then
begin
while CompareDateTime(StrToDateTime(ListView.Items.Item[Lo].Caption), Mid) > 0 do Inc(Lo);
while CompareDateTime(StrToDateTime(ListView.Items.Item[Hi].Caption), Mid) < 0 do Dec(Hi);
end
else
Dec(Hi);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi < iLo then QuickSortDateTimeAscend(ListView, Column, iLo, Hi, Selected);
if Lo > iHi then QuickSortDateTimeAscend(ListView, Column, Lo, iHi, Selected);
end
else
begin
Lo := iLo;
Hi := iHi;
if ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1] <> '' then
Mid := StrToDateTime(ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1])
else
Mid := MinDateTime;
repeat
if ListView.Items.Item[(Lo + Hi) div 2].SubItems[Column - 1] <> '' then
begin
while CompareDateTime(StrToDateTime(ListView.Items.Item[Lo].SubItems[Column - 1]), Mid) > 0 do Inc(Lo);
while CompareDateTime(StrToDateTime(ListView.Items.Item[Hi].SubItems[Column - 1]), Mid) < 0 do Dec(Hi);
end
else
Dec(Hi);
if Lo <= Hi then
begin
if Selected = True then
if Lo = ListView.Selected.Index then
ListView.Selected := ListView.Items[Hi]
else
if Hi = ListView.Selected.Index then
ListView.Selected := ListView.Items[Lo];
ListItemExchange(ListView.Items.Item[Lo], ListView.Items.Item[Hi], ListView.Columns.Count);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi < iLo then QuickSortDateTimeAscend(ListView, Column, iLo, Hi, Selected);
if Lo > iHi then QuickSortDateTimeAscend(ListView, Column, Lo, iHi, Selected);
end;
end;
 
对不起大家,因为我定的分数不是太多,但回答的我都总这能不给分吧,所以按回答的长短来分
配了,请大家还是多多包涵.下次我打算用1000分来出一个问题.但可能只会分配给一个人.呵呵
不过还是请前辈大哥们多多原谅.
 
后退
顶部