Einzelnen Beitrag anzeigen

wschrabi

Registriert seit: 16. Jan 2005
437 Beiträge
 
#1

Sortieren von STRINGGRID klappt nicht.

  Alt 20. Okt 2016, 12:33
Hallo Freunde,
habe Seattle und da will ich in meinem Prog bei klicken auf die Stringgridheaderzeile die Spalte richtig (dh bei Strings alfanumerisch bei ZAhlen nur numerisch) sortieren. Doch es klappt nicht. Weiß wer Rat? Besten DANK

Delphi-Quellcode:
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Grids;

type
  TForm1 = class(TForm)
    StringGrid9: TStringGrid;
    procedure FormCreate(Sender: TObject);
   procedure StringGrid9MouseUp(Sender: TObject; Button:
      TMouseButton; Shift: TShiftState; X, Y: Integer);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }

   procedure SortStringGrid(GenStrGrid: TStringGrid; ThatCol: Array of Integer);
    
  end;

var
  Form1: TForm1;
  function StrCmpLogical(const s1, s2: string): Integer;

implementation



{$R *.dfm}

function StrCmpLogicalW(psz1, psz2: PWideChar): Integer; stdcall; external 'shlwapi.dll';

function StrCmpLogical(const s1, s2: string): Integer;
begin
  //result:= StrCmpLogicalW(PWideChar(WideString(s1)), PWideChar(WideString(s2)));
  Result := StrCmpLogicalW(PChar(s1), PChar(s2));
end;

function CustomCompareLogical(List: TStringList;
  Index1, Index2: Integer): Integer;
var
   tmpstring1, tmpstring2: string;
   value1, value2: double;
begin

   tmpstring1:= Copy(List[index1],1,Pos('@',List[index1])-1);
   tmpstring2:= Copy(List[index2],1,Pos('@',List[index2])-1);
   if ((TryStrTofloat(tmpstring1, value1)) and (TryStrTofloat(tmpstring2, value2))) then
      begin
      if value1 > value2 then
            Result:=1
      else if value1 < value2 then
            result:=-1
      else if value1 = value2 then
            result:=0;
      end
  else
   Result := StrCmpLogical(List[Index1], List[Index2]);
  
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid9.Cells[0,0]:='Zeile';
  StringGrid9.Cells[1,0]:='Scopus Title';
  StringGrid9.Cells[2,0]:='similar Catalog Title';
  StringGrid9.Cells[3,0]:='Levensthein Distance';
  StringGrid9.Cells[4,0]:='rel. LS Distance [%]';
   StringGrid9.ColWidths[1]:=250;
   StringGrid9.ColWidths[2]:=250;
   StringGrid9.ColWidths[3]:=140;
   StringGrid9.ColWidths[4]:=140;

  StringGrid9.Cells[0,1]:='1';
  StringGrid9.Cells[1,1]:='AAAAAA Title';
  StringGrid9.Cells[2,1]:='similar Catalog Title';
  StringGrid9.Cells[3,1]:='1';
  StringGrid9.Cells[4,1]:='0,1';
  StringGrid9.Cells[0,2]:='2';
  StringGrid9.Cells[1,2]:='BBBBBB Title';
  StringGrid9.Cells[2,2]:='similar Catalog Title';
  StringGrid9.Cells[3,2]:='3';
  StringGrid9.Cells[4,2]:='0,3';
  StringGrid9.Cells[0,3]:='22';
  StringGrid9.Cells[1,3]:='CCCCCC Title';
  StringGrid9.Cells[2,3]:='similar Catalog Title';
  StringGrid9.Cells[3,3]:='22';
  StringGrid9.Cells[4,3]:='0,22';
  StringGrid9.Cells[0,4]:='3';
  StringGrid9.Cells[1,4]:='DDDDDD Title';
  StringGrid9.Cells[2,4]:='similar Catalog Title';
  StringGrid9.Cells[3,4]:='4';
  StringGrid9.Cells[4,4]:='0,4';
   
end;

procedure TForm1.StringGrid9MouseUp(Sender: TObject; Button:
TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  ACol, ARow: Integer;
begin
  StringGrid9.MouseToCell(x, y, ACol, ARow);
  if ((Arow=0) ) then
      begin
      SortStringGrid(Stringgrid9,ACol);
      end;
    
  (* 
  if ARow <= StringGrid9.FixedRows-1 then
    ShowMessage('Clicked on Fixed Row');
    *)

end;

procedure TForm1.SortStringGrid(GenStrGrid: TStringGrid; ThatCol: Array of Integer);
const
  // Define the Separator
  TheSeparator = '@';
var
  CountItem, I, J, K, ThePosition: integer;
  MyList: TStringList;
  MyString, TempString: string;
begin
  // Give the number of rows in the StringGrid
  CountItem := GenStrGrid.RowCount;
  //Create the List
  MyList := TStringList.Create;
  MyList.Sorted := False;
  try
    begin
    (*
      MyList.customsort(CustomCompareLogical);
      //Sort the List
      Mylist.Sort; *)

    
      for I := 1 to (CountItem - 1) do
        MyList.Add(GenStrGrid.Rows[I].Strings[ThatCol[0]] + TheSeparator +
          GenStrGrid.Rows[I].Text);
      MyList.customsort(CustomCompareLogical);
      //Sort the List
      Mylist.Sort;

      for K := 1 to Mylist.Count do
      begin
        //Take the String of the line (K – 1)
        MyString := MyList.Strings[(K - 1)];
        //Find the position of the Separator in the String
        ThePosition := Pos(TheSeparator, MyString);
        TempString := '';
        {Eliminate the Text of the column on which we have sorted the StringGrid}
        TempString := Copy(MyString, (ThePosition + 1), Length(MyString));
        MyList.Strings[(K - 1)] := '';
        MyList.Strings[(K - 1)] := TempString;
      end;

      // Refill the StringGrid
      for J := 1 to (CountItem - 1) do
        GenStrGrid.Rows[J].Text := MyList.Strings[(J - 1)];
        
        
    end;
  finally
    //Free the List
    MyList.Free;
  end;
end;



end.
Angehängte Dateien
Dateityp: zip bug.zip (53,1 KB, 0x aufgerufen)
  Mit Zitat antworten Zitat