我将ListView的ViewStyle设为vsReport,并设置了多列,如何使ListView中的项目按照用户点击的column排序?

解决方案 »

  1.   

    不清楚,但如果Column没有排序方法,就自己在OnColumnClick中重量对ListView的Column添加一遍。
      

  2.   

    两个事件~~~
    procedure Tgdzc_form.ListView1Compare(Sender: TObject; Item1,
      Item2: TListItem; Data: Integer; var Compare: Integer);
    var
      ix: Integer;
    begin
    ColumnToSort:=0;
      if ColumnToSort = 0 then
        Compare := CompareText(Item1.Caption,Item2.Caption)
      else begin
       ix := ColumnToSort - 1;
       Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix]);
      end;
      if SortStyle then
        Compare:=-Compare;
    end;procedure Tgdzc_form.ListView1ColumnClick(Sender: TObject;
      Column: TListColumn);
    begin
      ColumnToSort := Column.Index;
      (Sender as TCustomListView).AlphaSort;
      SortStyle:=not SortStyle;
    end;
      

  3.   

    这不是我写的,但是我在用:
    function CustSortAsc(Item1, Item2: TListItem;
      ColIndex: Integer): Integer; stdcall;
    var
      Item1Str: string;
      Item2Str: string;
      TmpExt: Extended;
    begin
      Result := 0;
      Item1Str := '';
      Item2Str := '';  if ColIndex < 0 then
        Exit;  if ColIndex = 0 then
      begin
        Item1Str := Item1.Caption;
        Item2Str := Item2.Caption;
      end
      else
      begin
        if ColIndex <= Item1.SubItems.Count then
          Item1Str := Item1.SubItems[ColIndex - 1];    if ColIndex <= Item2.SubItems.Count then
          Item2Str := Item2.SubItems[ColIndex - 1];
      end;  Result := CompareText(Item1Str, Item2Str);  // If one of the strings is empty, make the other string sort before it  if (Result > 0) and (Item2Str = '') then
        Result := -1
      else if (Result < 0) and (Item1Str = '') then
        Result := 1;
    end; {= CustSortAsc =}function CustSortDesc(Item1, Item2: TListItem;
      ColIndex: Integer): Integer; stdcall;
    begin
      Result := CustSortAsc(Item1, Item2, ColIndex);
      Result := Result * -1;
    end;procedure TForm1.LVColumnClick(Sender: TObject; Column: TListColumn);
    begin
      if FLastIndex = Column.Index then
      begin
        LV.CustomSort(@CustSortDesc, Column.Index);
        FLastIndex := -1;
      end
      else
      begin
        LV.CustomSort(@CustSortAsc, Column.Index);
        FLastIndex := Column.Index;
      end;
    end;
      

  4.   

    winsock2000(winsock)的方法有bug。 nobitian(nobility)的方法只能对字符串排序,要是其中有一列是其他数据类型比如整型(文件大小)、日期时间型(文件时间)有该如何排序呢?
      

  5.   

    区别对待。
    如果整数的列,可以对该列转换成整数后,再进行比较;
    如果是日期时间的,也可以转换成日期类型后,再进行比较。
    如果第二列是整数的
    procedure Tgdzc_form.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 if ColumnToSort=1 then
        begin
           if (strotint(Item1.sunitemItem1.SubItems[0])>(strtoint   
              (Item1.sunitemItem1.SubItems[0])) then
              Compare := 0
              else
                Compare :=1;  
        end else
       ............
       
    end;