我想请问当我点击listview上面每一列的标题时可以实现排序的功能
请问这样的功能是怎么实现的 在listview里面可以实现吗?
                                             哪位高手会的请指点
                                             小弟这里感谢了?

解决方案 »

  1.   

    首先将ListView对象的SortType属性设为stData然后声明两个全局变量来保存被单据的列的索引号和当前索引状态(ASC或DESC),操作如下:
    在你的单元接口段的private下声明:
    columntosort:integer;
    isascsort:boolean;最后,在ListView的ColumnClick事件里给columntosort赋值(被单击列的索引号),调用AlphaSort:
    procedure TForm1.ListView1ColumnClick(Sender: TObject;
      Column: TListColumn);
    begin
      isascsort:=not isascsort;
      columntosort:=column.Index;
      (sender as tcustomlistview).AlphaSort;
    end;然后在ListView的Compare事件里写排序代码:procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
      Data: Integer; var Compare: Integer);
    var
      xx:integer;
    begin
      if columntosort=0 then//按标题列排序;
        if isascsort then
          compare:=comparetext(item1.Caption,item2.Caption)
        else
          compare:=comparetext(item2.Caption,item1.Caption)
      else//按SubItems排序
      begin
        xx:=columntosort-1;
        if isascsort then
           compare:=comparetext(item1.SubItems[xx],item2.SubItems[xx])
        else
           compare:=comparetext(item2.SubItems[xx],item1.SubItems[xx]);
      end;
    end;
    你去试试看能不能符合你的要求。
      

  2.   

    procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
      Data: Integer; var Compare: Integer);
    var
      ix : Integer;
    begin  if cancompare then exit;
      if not SortStyle Then
        if ColumnToSort = 0 then
          Compare := CompareText(Item1.Caption,Item2.Caption)
        else begin
          ix := ColumnToSort - 1;
          Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix]);
        end
      Else
        if ColumnToSort = 0 then
          Compare := CompareText(Item2.Caption,Item1.Caption)
        else begin
          ix := ColumnToSort - 1;
          Compare := CompareText(Item2.SubItems[ix],Item1.SubItems[ix]);
        end;end;给分