Einzelnen Beitrag anzeigen

Bjoerk

Registriert seit: 28. Feb 2011
Ort: Mannheim
1.384 Beiträge
 
Delphi 10.4 Sydney
 
#7

AW: StringGrid nach Spalten aufsteigend und absteigend sortieren

  Alt 10. Aug 2011, 00:48
So ist es bei mir gelaufen.

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids;

type
  TDateSortFlags = (dsfKunde, dsfBedarf, dsfFertigungsauftrag, dsfArbeitsgang);

  TRecord = Record
    Kunde: string;
    Datum: TDateTime;
    Bedarf, Fertigungsauftrag, Arbeitsgang: integer;
  end;

  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    procedure SortByDate (const Jahr: string; const dsf: TDateSortFlags);
    procedure SetRow (const I: integer; const ARecord: TRecord; const Jahr: string);
    function GetRow (const I: integer; const Jahr: string): TRecord;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


function TForm1.GetRow (const I: integer; const Jahr: string): TRecord;
begin
  with Result, StringGrid1 do
  begin
    if Jahr <> 'then
      Datum:= StrToDateTime(Cells[FixedCols, I]+Jahr)
    else
      Datum:= StrToDateTime(Cells[FixedCols, I]);

    Kunde:= Cells[FixedCols+1, I];

    Bedarf:= StrToIntDef(Cells[FixedCols+2, I], -1);
    Fertigungsauftrag:= StrToIntDef(Cells[FixedCols+3, I], -1);
    Arbeitsgang:= StrToIntDef(Cells[FixedCols+4, I], -1);
  end;
end;


procedure TForm1.SetRow (const I: integer; const ARecord: TRecord; const Jahr: string);
begin
  with ARecord, StringGrid1 do
  begin
    if Jahr <> 'then
      Cells[FixedCols, I]:= FormatDateTime('dd.mm.', Datum)
    else
      Cells[FixedCols, I]:= FormatDateTime('dd.mm.yyyy', Datum);

    Cells[FixedCols+1, I]:= Kunde;

    if Bedarf < 0 then
      Cells[FixedCols+2, I]:= '-'
    else
      Cells[FixedCols+2, I]:= IntToStr(Bedarf);

    if Fertigungsauftrag < 0 then
      Cells[FixedCols+3, I]:= '-'
    else
      Cells[FixedCols+3, I]:= IntToStr(Fertigungsauftrag);

    if Arbeitsgang < 0 then
      Cells[FixedCols+4, I]:= '-'
    else
      Cells[FixedCols+4, I]:= IntToStr(Arbeitsgang);
  end;
end;


procedure TForm1.SortByDate (const Jahr: string; const dsf: TDateSortFlags);
var
  I, J: integer;
  T1, T2: TRecord;
  ExChange: boolean;
begin
  for I:= StringGrid1.FixedRows to StringGrid1.RowCount-2 do
    for J:= I+1 to StringGrid1.RowCount-1 do
    begin
      ExChange:= false;
      T1:= GetRow(I, Jahr);
      T2:= GetRow(J, Jahr);

      if T1.Datum > T2.Datum then // up
        ExChange:= true
      else
        if T1.Datum = T2.Datum then
        begin
          if dsf = dsfKunde then
            if T1.Kunde > T2.Kunde then ExChange:= true; // up

          if dsf = dsfBedarf then
            if T1.Bedarf < T2.Bedarf then ExChange:= true; // down

          if dsf = dsfFertigungsauftrag then
            if T1.Fertigungsauftrag < T2.Fertigungsauftrag then ExChange:= true; // down

          if dsf = dsfArbeitsgang then
            if T1.Arbeitsgang < T2.Arbeitsgang then ExChange:= true; // down
        end;
      if ExChange then
      begin
        SetRow(I, T2, Jahr);
        SetRow(J, T1, Jahr);
      end;
    end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  SortByDate ('2011', dsfFertigungsauftrag);
end;


procedure TForm1.FormCreate(Sender: TObject);
var
  I, J, K: integer;
begin
  randomize;
  with StringGrid1 do
  begin
    for I:= FixedRows to RowCount-1 do
      for J:= FixedCols to ColCount-1 do
        Cells[J, I]:= '-';

    K:= FixedCols;
    Cells[K, FixedRows]:= '15.08.';
    Cells[K, FixedRows+1]:= '15.08.';
    Cells[K, FixedRows+2]:= '16.08.';
    Cells[K, FixedRows+3]:= '16.08.';
    Cells[K, FixedRows+4]:= '18.08.';
    Cells[K, FixedRows+5]:= '18.08.';
    Cells[K, FixedRows+6]:= '18.08.';
    Cells[K, FixedRows+7]:= '19.08.';
    Cells[K, FixedRows+8]:= '19.08.';

    K:= FixedCols+1;
    for J:= FixedRows to RowCount-1 do
      Cells[K, J]:= IntToStr(RowCount-1-J);

    K:= FixedCols+2;
    Cells[K, FixedRows+2]:= '100';
    Cells[K, FixedRows+3]:= '200';
    Cells[K, FixedRows+7]:= '250';
    Cells[K, FixedRows+8]:= '180';

    K:= FixedCols+3;
    Cells[K, FixedRows]:= '100';
    Cells[K, FixedRows+1]:= '123';
    Cells[K, FixedRows+4]:= '119';
    Cells[K, FixedRows+5]:= '125';
    Cells[K, FixedRows+6]:= '124';

    K:= FixedCols+4;
    Cells[K, FixedRows]:= '50';
    Cells[K, FixedRows+1]:= '60';
    Cells[K, FixedRows+4]:= '40';
    Cells[K, FixedRows+5]:= '20';
    Cells[K, FixedRows+6]:= '30';
  end;
end;

end.
  Mit Zitat antworten Zitat