一个关于listview的排序的问题

alphen 2001-07-20 12:26:40
在listview中,如何使数据按照用户点击的Column排序
...全文
130 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
happybachelor 2001-10-19
  • 打赏
  • 举报
回复
up
bomdy 2001-07-23
  • 打赏
  • 举报
回复
to Alphen
不好意思,我几天没上网,现在才给你答复。
uses Commctrl;
在比较函数中,你可以根据需要对各列的内容进行类型转换,然后进行比较。
Crob 2001-07-20
  • 打赏
  • 举报
回复
function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
var txt1,txt2 : string;
begin
if ParamSort <> 0 then begin
txt1 := Item1.SubItems.Strings[ParamSort - 1];
txt2 := Item2.SubItems.Strings[ParamSort - 1];
Result := +CompareText(Item1.Caption,Item2.Caption);
end;
end;

//调用
procedure TmForm.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
begin
ListView1.CustomSort(@CustomSortProc, Column.Index);
end;
Crob 2001-07-20
  • 打赏
  • 举报
回复
//建回调函数
function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
var txt1,txt2 : string;
begin
if ParamSort <> 0 then begin
txt1 := Item1.SubItems.Strings[ParamSort - 1];
txt2 := Item2.SubItems.Strings[ParamSort - 1];
Result := CompareText(txt1,txt2);
end else begin
Result := -CompareText(txt1,txt2);
end;
end else begin
if m_bSort then begin
Result := CompareText(Item1.Caption,Item2.Caption);
end else begin
Result := -CompareText(Item1.Caption,Item2.Caption);
end;
end;
end;

//调用
procedure TmForm.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
begin
ListView1.CustomSort(@CustomSortProc, Column.Index);
end;
lwk_hlj 2001-07-20
  • 打赏
  • 举报
回复
关注
alphen 2001-07-20
  • 打赏
  • 举报
回复
to bomdy()
程序运行时,系统报TLVCompare没有声明,我该添加那个单元库
bomdy 2001-07-20
  • 打赏
  • 举报
回复
在ListView的OnColumnClick中调用它的CustomSort函数,为每个列定义一个TLVCompare排序方法,根据Column.Index进行选用,还可以通过Column.Tag决定排序的升降顺序:

function SortByCaption(Item1, Item2: TListItem;
ParamSort: integer): integer; stdcall;
begin
Result := compare(item1.caption, item2.caption);
if ParamSort=0 then Result := -Result;
end;

procedure TForm1.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
var
SortProc: TLVCompare;
begin
Column.Tag := (Column.Tag +1)mod 2;
case Column.Index of
0: SortProc := @SortByColumnCaption;
1: SortProc := @SortByColumnSubitem0;
2: SortProc := @SortByColumnSubitem1;
else SortProc := nil;
end;
if Assigned(SortProc) then
ListView1.CustomSort(SortProc, Column.Tag);
end;

5,391

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧