unit UCReport;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ExtCtrls, StdCtrls, Buttons, Math;
---
type
TFrmReport = class(TForm)
Panel1: TPanel;
LvReport: TListView;
BitBtn1: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
procedure LvReportCompare(Sender: TObject;
Item1, Item2: TListItem;
Data: Integer;
var Compare: Integer);
procedure LvReportColumnClick(Sender: TObject;
Column: TListColumn);
private
{ Private declarations }
public
{ Public declarations }
columntosort :integer;
//选中列的索引号
isascsort :boolean;
//是否升序
end;
var
FrmReport: TFrmStatusReport;
implementation
{$R *.dfm}
procedure TFrmReport.BitBtn1Click(Sender: TObject);
begin
Close;
end;
procedure TFrmReport.LvReportCompare(Sender: TObject;
Item1,
Item2: TListItem;
Data: Integer;
var Compare: Integer);
var //自定义排序号方式;
xi:integer;
begin
if columntosort=0 then
if isascsort then
compare:=Comparetext(item1.Caption,item2.Caption)
else
compare:=Comparetext(item2.Caption,item1.Caption)
else
begin
xi := columntosort-1;
if isascsort then
compare:=CompareText(item1.SubItems[xi],item2.SubItems[xi])
else
compare:=CompareText(item2.SubItems[xi],item1.SubItems[xi]);
end;
end;
procedure TFrmStatusReport.LvReportColumnClick(Sender: TObject;
Column: TListColumn);
var
i : integer;
begin
isascsort:=not isascsort;//判断排序方式,每次按下总是反向排序;
//当列不是当前列时,去掉列标题中的箭头。去掉所有的箭头,防止重复添加
for i := 0 to LvReport.Columns.Count -1do
// if LvReport.Column.Index <> Column.Index then
begin
LvReport.Columns.Caption := StringReplace(LvReport.Columns.Caption,
'▲ ','',[rfReplaceAll]);
LvReport.Columns.Caption :=StringReplace(LvReport.Columns.Caption,
'▼ ' ,'',[rfReplaceAll]);
end;
//为列标题添加箭头
if isAscSort then
(Sender as TListView).Columns[Column.Index].Caption :=
'▲ ' + (Sender as TListView).Columns[Column.Index].DisplayName
else
(Sender as TListView).Columns[Column.Index].Caption :=
'▼ ' + (Sender as TListView).Columns[Column.Index].DisplayName ;
columntosort:=column.Index;
(sender as tcustomlistview).AlphaSort;
end;
end.