RT

解决方案 »

  1.   

    是不是要排序
    要排序请用以下代码
    首先声明两个全局变量来保存被单据的列的索引号和当前索引状态(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;以上须注意排序原理,相应函数可查DELPHI帮助.