Thema: Delphi 2 Stellen im Stringgrid

Einzelnen Beitrag anzeigen

grayfox

Registriert seit: 22. Jan 2003
Ort: Litschau
173 Beiträge
 
Delphi 4 Standard
 
#17

Re: 2 Stellen im Stringgrid

  Alt 10. Mär 2004, 20:37
hallo hansa!
sieh dir das mal an:

Delphi-Quellcode:
procedure TAuftragDlg.PosGridDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  CellText: String;
  tw: ShortInt;
  Zahlwert: Double;
begin
  with Sender as TStringGrid do begin
    if not (gdFixed in State) then begin
      if (aRow mod 2 = 0) then begin
        Canvas.Brush.Color:= $00FDFACE;
        Canvas.Font.Color:= clBlack
      end else begin
        Canvas.Brush.Color:= $00CEFFFD;
        Canvas.Font.Color:= clBlack
      end
    end else begin
      Canvas.Brush.Color:= clBtnFace;
      Canvas.Font.Color:= clBlack;
    end;

    CellText:= trim(Cells[aCol,aRow]);

    if (aRow > 0) and (aCol in [0,4..17]) then begin
      try
        if CellText= 'then
          Zahlwert:= 0
        else
          Zahlwert:= StrToFloat(CellText);
        case aCol of
          4,7,8,9,12,15 : FNumberFormat:= '#';
          5 : FNumberFormat:= '#0.000';
          6,10,11,13,14,16,17: FNumberFormat:= '#,##0.00';
        end;

        if Zahlwert > 0 then
          CellText:= FormatFloat(FNumberFormat,ZahlWert)
            else
          CellText:= '';
      except
        CellText:= '';
      end;
    end;

    if Cells[1,aRow] = ' - then
      Cells[1,aRow]:= '';
    if Cells[1,aRow] <> 'then begin
      tw:= Canvas.TextWidth(CellText);
      Canvas.FillRect(Rect);

      if (aRow = 0) then
        Canvas.TextOut(Rect.Left + (Rect.Right-Rect.Left-tw) div 2, Rect.Top+2, CellText)
      else if aCol in [0,4..17] then
        Canvas.TextOut(Rect.Right -2 - tw, Rect.Top + 2, CellText)
      else
        Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, CellText);
    end;
  end;
end;
hier gehts um ein stringgrid mit einer ganzen menge spalten, mit 3 verschiedenen zahlenformaten.
das ganze wird dann noch entweder links, rechts oder mittig ausgerichtet.

vielleicht ist es etwas umständlich geschrieben, aber es funktioniert

mfg, stefan
  Mit Zitat antworten Zitat