两个事件~~~ procedure Tgdzc_form.ListView1Compare(Sender: TObject; Item1, Item2: TListItem; Data: Integer; var Compare: Integer); var ix: Integer; begin ColumnToSort:=0; if ColumnToSort = 0 then Compare := CompareText(Item1.Caption,Item2.Caption) else begin ix := ColumnToSort - 1; Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix]); end; if SortStyle then Compare:=-Compare; end;procedure Tgdzc_form.ListView1ColumnClick(Sender: TObject; Column: TListColumn); begin ColumnToSort := Column.Index; (Sender as TCustomListView).AlphaSort; SortStyle:=not SortStyle; end;
这不是我写的,但是我在用: function CustSortAsc(Item1, Item2: TListItem; ColIndex: Integer): Integer; stdcall; var Item1Str: string; Item2Str: string; TmpExt: Extended; begin Result := 0; Item1Str := ''; Item2Str := ''; if ColIndex < 0 then Exit; if ColIndex = 0 then begin Item1Str := Item1.Caption; Item2Str := Item2.Caption; end else begin if ColIndex <= Item1.SubItems.Count then Item1Str := Item1.SubItems[ColIndex - 1]; if ColIndex <= Item2.SubItems.Count then Item2Str := Item2.SubItems[ColIndex - 1]; end; Result := CompareText(Item1Str, Item2Str); // If one of the strings is empty, make the other string sort before it if (Result > 0) and (Item2Str = '') then Result := -1 else if (Result < 0) and (Item1Str = '') then Result := 1; end; {= CustSortAsc =}function CustSortDesc(Item1, Item2: TListItem; ColIndex: Integer): Integer; stdcall; begin Result := CustSortAsc(Item1, Item2, ColIndex); Result := Result * -1; end;procedure TForm1.LVColumnClick(Sender: TObject; Column: TListColumn); begin if FLastIndex = Column.Index then begin LV.CustomSort(@CustSortDesc, Column.Index); FLastIndex := -1; end else begin LV.CustomSort(@CustSortAsc, Column.Index); FLastIndex := Column.Index; end; end;
区别对待。 如果整数的列,可以对该列转换成整数后,再进行比较; 如果是日期时间的,也可以转换成日期类型后,再进行比较。 如果第二列是整数的 procedure Tgdzc_form.ListView1Compare(Sender: TObject; Item1, Item2: TListItem; Data: Integer; var Compare: Integer); var ix: Integer; begin if ColumnToSort = 0 then Compare := CompareText(Item1.Caption,Item2.Caption) else if ColumnToSort=1 then begin if (strotint(Item1.sunitemItem1.SubItems[0])>(strtoint (Item1.sunitemItem1.SubItems[0])) then Compare := 0 else Compare :=1; end else ............
procedure Tgdzc_form.ListView1Compare(Sender: TObject; Item1,
Item2: TListItem; Data: Integer; var Compare: Integer);
var
ix: Integer;
begin
ColumnToSort:=0;
if ColumnToSort = 0 then
Compare := CompareText(Item1.Caption,Item2.Caption)
else begin
ix := ColumnToSort - 1;
Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix]);
end;
if SortStyle then
Compare:=-Compare;
end;procedure Tgdzc_form.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
begin
ColumnToSort := Column.Index;
(Sender as TCustomListView).AlphaSort;
SortStyle:=not SortStyle;
end;
function CustSortAsc(Item1, Item2: TListItem;
ColIndex: Integer): Integer; stdcall;
var
Item1Str: string;
Item2Str: string;
TmpExt: Extended;
begin
Result := 0;
Item1Str := '';
Item2Str := ''; if ColIndex < 0 then
Exit; if ColIndex = 0 then
begin
Item1Str := Item1.Caption;
Item2Str := Item2.Caption;
end
else
begin
if ColIndex <= Item1.SubItems.Count then
Item1Str := Item1.SubItems[ColIndex - 1]; if ColIndex <= Item2.SubItems.Count then
Item2Str := Item2.SubItems[ColIndex - 1];
end; Result := CompareText(Item1Str, Item2Str); // If one of the strings is empty, make the other string sort before it if (Result > 0) and (Item2Str = '') then
Result := -1
else if (Result < 0) and (Item1Str = '') then
Result := 1;
end; {= CustSortAsc =}function CustSortDesc(Item1, Item2: TListItem;
ColIndex: Integer): Integer; stdcall;
begin
Result := CustSortAsc(Item1, Item2, ColIndex);
Result := Result * -1;
end;procedure TForm1.LVColumnClick(Sender: TObject; Column: TListColumn);
begin
if FLastIndex = Column.Index then
begin
LV.CustomSort(@CustSortDesc, Column.Index);
FLastIndex := -1;
end
else
begin
LV.CustomSort(@CustSortAsc, Column.Index);
FLastIndex := Column.Index;
end;
end;
如果整数的列,可以对该列转换成整数后,再进行比较;
如果是日期时间的,也可以转换成日期类型后,再进行比较。
如果第二列是整数的
procedure Tgdzc_form.ListView1Compare(Sender: TObject; Item1,
Item2: TListItem; Data: Integer; var Compare: Integer);
var
ix: Integer;
begin if ColumnToSort = 0 then
Compare := CompareText(Item1.Caption,Item2.Caption)
else if ColumnToSort=1 then
begin
if (strotint(Item1.sunitemItem1.SubItems[0])>(strtoint
(Item1.sunitemItem1.SubItems[0])) then
Compare := 0
else
Compare :=1;
end else
............
end;