Align number at right in a StrinGrid

 



Procedure TForm1.StringGrid1DrawCell ( Sender : TObject ; ACol , ARow : Integer ;

   Rect : TRect ; State : TGridDrawState ) ;

Var

   sCad        : String ;

   i           : integer ;

   bEsNum      : Bool ;

Begin

   If StringGrid1.Cells[ aCol , aRow ] <> '' Then

   Begin

      bEsNum := True ;

      sCad := StringGrid1.Cells[ aCol , aRow ] ;

      For i := 1 To length ( sCad ) Do

         If Not ( sCad[ i ] In [ '0'..'9' , '-' , ' ' ] ) Then bEsNum := False ;

      If bEsNum Then

      Begin

         sCad := Format ( '%15.0n' , [ StrToFloat ( StringGrid1.Cells[ aCol , aRow ] ) ] ) ;

         With StringGrid1 Do

            With Canvas , Rect Do

            Begin

               i := Right - TextWidth ( sCad + ' ' ) ;

               Canvas.FillRect ( Rect ) ;

               Canvas.TextOut ( i , Top + 2 , sCad ) ;

            End ;

      End ;

   End ;

End ;

 

To enable it :

 

Procedure TForm1.Button1Click ( Sender : TObject ) ;

Begin

   StringGrid1.Cells[ 0 , 1 ] := 'dsffd 1' ;

   StringGrid1.Cells[ 1 , 1 ] := ' 1001' ;

   StringGrid1.Cells[ 2 , 3 ] := ' 4343241 ' ;

   StringGrid1.Cells[ 3 , 2 ] := '123 hjfh' ;

End ;


Back Home Foward