Displaying vertical lines in TStringGrid when default drawing = false

When I turn off default drawing for a TStringGrid, I lose the vertical and horizontal lines.

How do you bring these back?
GrahamDLovellAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gxsCommented:
In order to do that to components such as the TStringGrid and TDataGrid, you have to use the OnDrawCell event.

Don't forget that you have set Default Drawing = False which means "Delphi, Let me handle the lines manually"

Anyway, here is a simple example for the string grid:

http://delphi.about.com/od/vclusing/a/owner-drawing-delphi-string-grid.htm
0
GrahamDLovellAuthor Commented:
Thanks for your response.

I am across using OnDrawCell event, but I can't see that your example actually addresses my problem.

I hope I don't have to create multiple resource images (one for each rectangle size) just to put the vertical and horizontal lines back in place.
0
Pierre CorneliusCommented:
Which version of delphi are you using?

When I set defaultdrawing to false, it still draws the lines.

Can you check your StringGrid.Options and look if the following are set to true: goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine
0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

GrahamDLovellAuthor Commented:
Delphi XE4

Re: goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine

Yes, they are the options that are set. They are the defaults, so they don't appear in the object text listing, but I can make them appear by turning off goRangeSelect. Either way, they don't appear when the program runs.

Here is an extract from the "Form as Text" for this TStringGrid:

  object sgPay: TStringGrid
    Left = 8
    Top = 200
    Width = 769
    Height = 213
    ColCount = 13
    DefaultRowHeight = 20
    DefaultDrawing = False
    FixedCols = 0
    RowCount = 2
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    TabOrder = 5
    OnDrawCell = sgPayDrawCell
    OnExit = sgPayExit
    OnKeyPress = sgPayKeyPress
    OnSelectCell = sgPaySelectCell
    ColWidths = (
      148
      70
      70
      120
      80
      60
      80
      60
      24
      24
      0
      0
      0)
  end

Open in new window


Also, if it helps here is my DrawCell code:

procedure TfPayment.sgPayDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  SavedAlign: Word;
  SaveString: String;
begin
  with sgPay do
  begin
    Canvas.Rectangle(Rect);
    if (ACol = 6) and
       (ARow <> 0) then
    begin
      if (Cells[ACol,ARow] <> '') then
         SaveString := fMain.AddCents(Cells[ACol,ARow]);
    end
    else
      SaveString := Cells[ACol,ARow];
    if ARow = 0 then
      canvas.Brush.Color := FixedColor
    else
      Canvas.Brush.Color := Color;
    if (ACol < 4) then        {Left Align}
    begin
      SavedAlign := SetTextAlign(Canvas.Handle, TA_Left);
      Canvas.TextRect(Rect,
        Rect.Left + 2, Rect.Top, SaveString);
      SetTextAlign(Canvas.Handle, SavedAlign);
    end
    else
    begin
      SavedAlign := SetTextAlign(Canvas.Handle, TA_RIGHT);
      Canvas.TextRect(Rect,
        Rect.Right - 2, Rect.Top, SaveString);
      SetTextAlign(Canvas.Handle, SavedAlign);
    end;
  end;
end;

Open in new window

0
Pierre CorneliusCommented:
I suspect you are drawing over the lines. Try removing the following from your drawcell:
Canvas.rectangle(rect);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GrahamDLovellAuthor Commented:
I noticed (after I posted) that I had left part of the residue of my earlier attempt to fix this problem in the code.

Removing it does not help.

Seeing that I am not doing anything that is obviously wrong, in a new attempt to fix it, I will replace my drawcell code with one based on your (earlier) code example, and see whether that works better.

I will post here when I have done this.

Thanks for your help so far.
0
ThommyCommented:
Turning off Defaultdrawing does not affect drawing of horizontal and vertical lines at all!
It only controls the drawing of the grid cells themselves.

The grid behavior is controlled by the property Options.

To switch grid lines on/off use below options:

goVertLine
goHorzLine
goFixedVertLine
goFixedHorzLine
0
GrahamDLovellAuthor Commented:
PierreC:

It wasn't Canvas.rectangle(rect) line that was causing the damage, although I consider you were on the right track.

After much testing, I found the problem. I had used the TStringGrid.selection setting of  (Left:0; Top:1; Right:0; Bottom:1 ). This was legacy from another attempted fix. When I removed it, the vertical and horizontal lines returned.

Phew!
0
GrahamDLovellAuthor Commented:
PierreC:

I was wrong again!

I had actually programatically reset the options to [GoEditing, GoTabs], not realising that this actually killed off all of the other selections.

So much to learn, so little time!
0
Pierre CorneliusCommented:
Hi graham. Im glad to hear you got it working. Sorry i could not get get back here sooner, i had a major crisis at work this week.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.