Re: Right align column of Delphi stringgrid

Posted by Jenny on September 15, 2005

In Reply to: help, how to align first column of stringgrid? posted by robert on September 13, 2005

: I need write the numbers of rows in the first column of a string grid how a spreadsheet

: I use the following piece of code to write floats in the other columns but i don't know how write integers.

: if Grid.Cells[ACol,ARow] <> '' then begin
: bEsNum := True;
: sCad := Grid.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('%10.2f',[StrToFloat(Grid.Cells[ACol,ARow])]);
: with Grid.Canvas,Rect do begin
: i:=Right-TextWidth(sCad+' ');
: Grid.Canvas.FillRect(Rect);
: Grid.Canvas.TextOut(i,Top+2,sCad);
: end;
: end;
: end;

The first column is number 0. And let's suppose that you have one fixed row at the top.
You can fill the first column in one go, like this, for example in the OnCreate event of the Form:

var R: integer;
 begin
   for R := 1 to Grid.RowCount - 1 do
     Grid.Cells[0, R] := IntToStr(R); 

However, this does not right-align these numbers. If you also want to right-align column 0, just add 1 line to your OnDraw event handler:

if Grid.Cells[ACol,ARow] <> '' then begin
   bEsNum := True;
   sCad := Grid.Cells[ACol,ARow];
   if ACol > 0 then // add this line
     // and leave the rest as it was 

Succes!
Jenny

Related Articles and Replies