同上!

解决方案 »

  1.   

    procedure TFra_Change.LvSourceColumnClick(Sender: TObject;
      Column: TListColumn);
    begin
        ColumntoSort:=column.Index;
        (Sender as TCustomListView).AlphaSort;
    end;procedure TFra_Change.LvSourceCompare(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;
      

  2.   

    首先定义一个全局变量:
    var ColumnToSort: Integer;
    然后添加ListView对象的OnColumnClick事件:
    procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn);begin
      ColumnToSort := Column.Index;
      (Sender as TCustomListView).AlphaSort;
    end;
    最后添加ListView对象的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;
      

  3.   

    listView排序2首先声明两个全局变量来保存被单据的列的索引号和当前索引状态(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;
      

  4.   

    补充一点,使用我的方法进行排序的时候请将ListView对象的SortType属性设为stData。
      

  5.   

    houwei1008(下雨天)的方法非常可行
      

  6.   

    刚才我去试了一下(下雨天)写的,可以排序,但是我们点某一列的时候应该出现一个上下的键诚头,就像其他的软件一样的效果,不知道listview能不能完成这样的效果
      

  7.   

    回复人: wlmy2004(wl) ( ) 信誉:97  2004-07-01 15:55:00  得分: 0  
     
     
       刚才我去试了一下(下雨天)写的,可以排序,但是我们点某一列的时候应该出现一个上下的键诚头,就像其他的软件一样的效果,不知道listview能不能完成这样的效果
      
     
    ----------------
    请看
    http://community.csdn.net/Expert/topic/3042/3042049.xml?temp=.4693872