Re: StringGrid Vertical Lines

Posted by John, DelphiLand Team on June 21, 2008

In Reply to StringGrid Vertical Lines posted by Peter Howes on June 19, 2008

: I have a 16 column StringGrid and would like to add vertical separator lines.
: This is straightfoward for all columns but I only want the lines to appear between columns 5 and 6, and between 12 and 13.
: Is there a way to do this in Delphi4.0?

If you want to disable vertical grid lines between most of the columns of the StringGrid, except for a few ones, disable the following Grid.Options (in the Object Inspector, set them to "false"):

- goFixedVertLine: draws a vertical line to the right of cells in the "fixed" area;
- goVertLine: draws a vertical line to the right of cells in the "browseable" area.

At runtime, we'll draw a vertical line in the desired columns. In your case, that would be columns 5 and 12. We accomplish this by adding an OnDrawCell handler to the StringGrid.

If an OnDrawCell handler exists, Delphi calls that procedure for custom cell painting. So let's write source code for that event, adding a vertical line to the right of columns 5 and 12:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
  if (ACol = 5) or (ACol = 12) then begin
    // clSilver or whatever color you want for vertical grid lines:
    StringGrid1.Canvas.Pen.Color := clSilver;
    // Draw a line from the cell's top-right to its bottom-right:
    StringGrid1.Canvas.MoveTo(Rect.Right - 1, Rect.Top);
    StringGrid1.Canvas.LineTo(Rect.Right - 1, Rect.Bottom);

Good luck, and call back if you want more info!
John, DelphiLand Team

Related articles

Annoying StringGrid Focus
... setting the focus on the upper lefthand cell [0,0] and coloring it dark blue. This obfuscates what i want to show in that cell...
Change cell colours
How to set up arrays for FG and BG colours of stringgrid cells
Coloured text in stringgrid
Put differently coloured strings *within one cell*: part 1 red, part 2 blue,...

Follow Ups