1、 procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn);begin ColumnToSort := Column.Index; (Sender as TCustomListView).AlphaSort; end; ///////////////// 2、 var Ln: TListItem; begin Ln:=TListItem.Create(ListView1.Items); With ListView1 do begin Ln.Assign(Items[0]); Items[0].Assign(Items[Items.Count-1]); Items[Items.Count-1].Assign(Ln); end; end;
procedure TForm1.Lv_2ColumnClick(Sender: TObject; Column: TListColumn); begin nsortcol:=column.Index; //排序 JK[nsortcol]:=(JK[nsortcol]+1) mod 2;//这里是我自已定义的一个升降序 lv_2.AlphaSort; end;procedure TFrm_main.Lv_2Compare(Sender: TObject; Item1, Item2: TListItem; Data: Integer; var Compare: Integer); var DD1,DD2:TDateTime; RR1,RR2:Real; begin //可分两种情况进行排序 case nsortcol of 0://Caption处排序 begin //进行行排序的比较 if JK[0]=0 then compare:=Comparestr(item1.Caption,item2.Caption) else compare:=Comparestr(item2.Caption,item1.Caption); end; 1://字符串 begin if JK[1]=0 then compare:=comparestr(item2.SubItems[nsortcol-1],item1.SubItems[nsortcol-1]) else compare:=comparestr(item1.SubItems[nsortcol-1],item2.SubItems[nsortcol-1]); end; 2://时间------可自行修改 begin if trim(item2.SubItems[nsortcol-1])='' then DD1:=strtodatetime('1990-01-01 01:01:01') else DD1:=strtodatetime(item2.SubItems[nsortcol-1]); if trim(item1.SubItems[nsortcol-1])='' then DD2:=strtodatetime('1990-01-01 01:01:01') else DD2:=strtodatetime(item1.SubItems[nsortcol-1]); if JK[2]=0 then compare:=comparedatetime(DD1,DD2) else compare:=comparedatetime(DD2,DD1); end; 5://数字 begin if trim(item1.SubItems[nsortcol-1])='' then RR1:=0 else RR1:=strtofloat(trim(copy(item1.SubItems[nsortcol-1],3,length(item1.SubItems[nsortcol-1])-2))); if trim(item2.SubItems[nsortcol-1])='' then RR2:=0 else RR2:=strtofloat(trim(copy(item2.SubItems[nsortcol-1],3,length(item2.SubItems[nsortcol-1])-2))); if JK[nsortcol]=0 then compare:=CompareValue(RR1,RR2) else compare:=CompareValue(RR2,RR1); end; else begin if JK[nsortcol]=0 then compare:=comparestr(item2.SubItems[nsortcol-1],item1.SubItems[nsortcol-1]) else compare:=comparestr(item1.SubItems[nsortcol-1],item2.SubItems[nsortcol-1]); end; end; end;以上是我程序的一部分,可能比较乱,但应该可以给你提示,因为ListView中有数字或日期你不能直接按字符串排序的
如果是的话,在往listview中添加的时候,首先,把数据表中按照某个字段排序,这样listview中的数据不久排序了嘛
var ColumnToSort: Integer;//在listview的columnclick事件赋值,并调用alphasort
procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn);begin
ColumnToSort := Column.Index;
(Sender as TCustomListView).AlphaSort;
end;//oncompare事件
procedure TForm1.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 begin
ix := ColumnToSort - 1;
Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix]);
end;end;
var ColumnToSort: Integer;//在listview的columnclick事件赋值,并调用alphasort
procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn);begin
ColumnToSort := Column.Index;
(Sender as TCustomListView).AlphaSort;
end;//oncompare事件
procedure TForm1.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 begin
ix := ColumnToSort - 1;
Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix]);
end;end;
根据这个变量的值将
Compare := CompareText(Item1.Caption,Item2.Caption)
句中的item1和item2颠倒位置即可。
procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn);begin
ColumnToSort := Column.Index;
(Sender as TCustomListView).AlphaSort;
end;
/////////////////
2、
var
Ln: TListItem;
begin
Ln:=TListItem.Create(ListView1.Items);
With ListView1 do
begin
Ln.Assign(Items[0]);
Items[0].Assign(Items[Items.Count-1]);
Items[Items.Count-1].Assign(Ln);
end;
end;
begin
nsortcol:=column.Index; //排序
JK[nsortcol]:=(JK[nsortcol]+1) mod 2;//这里是我自已定义的一个升降序
lv_2.AlphaSort;
end;procedure TFrm_main.Lv_2Compare(Sender: TObject; Item1, Item2: TListItem;
Data: Integer; var Compare: Integer);
var
DD1,DD2:TDateTime;
RR1,RR2:Real;
begin
//可分两种情况进行排序
case nsortcol of
0://Caption处排序
begin
//进行行排序的比较
if JK[0]=0 then
compare:=Comparestr(item1.Caption,item2.Caption)
else
compare:=Comparestr(item2.Caption,item1.Caption);
end;
1://字符串
begin
if JK[1]=0 then
compare:=comparestr(item2.SubItems[nsortcol-1],item1.SubItems[nsortcol-1])
else
compare:=comparestr(item1.SubItems[nsortcol-1],item2.SubItems[nsortcol-1]);
end;
2://时间------可自行修改
begin
if trim(item2.SubItems[nsortcol-1])='' then
DD1:=strtodatetime('1990-01-01 01:01:01')
else
DD1:=strtodatetime(item2.SubItems[nsortcol-1]);
if trim(item1.SubItems[nsortcol-1])='' then
DD2:=strtodatetime('1990-01-01 01:01:01')
else
DD2:=strtodatetime(item1.SubItems[nsortcol-1]);
if JK[2]=0 then
compare:=comparedatetime(DD1,DD2)
else
compare:=comparedatetime(DD2,DD1);
end;
5://数字
begin
if trim(item1.SubItems[nsortcol-1])='' then RR1:=0
else RR1:=strtofloat(trim(copy(item1.SubItems[nsortcol-1],3,length(item1.SubItems[nsortcol-1])-2)));
if trim(item2.SubItems[nsortcol-1])='' then RR2:=0
else RR2:=strtofloat(trim(copy(item2.SubItems[nsortcol-1],3,length(item2.SubItems[nsortcol-1])-2)));
if JK[nsortcol]=0 then
compare:=CompareValue(RR1,RR2)
else
compare:=CompareValue(RR2,RR1);
end;
else
begin
if JK[nsortcol]=0 then
compare:=comparestr(item2.SubItems[nsortcol-1],item1.SubItems[nsortcol-1])
else
compare:=comparestr(item1.SubItems[nsortcol-1],item2.SubItems[nsortcol-1]);
end;
end;
end;以上是我程序的一部分,可能比较乱,但应该可以给你提示,因为ListView中有数字或日期你不能直接按字符串排序的