Jump to content
aehimself

Styled DBGrid OnDrawDataCell

Recommended Posts

Hello,

 

In Delphi 10.3.3 I have a DBGrid component with the following handler:

procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState);
Var
 r: TRect;
begin
 If ZQuery1.RecNo Mod 2 = 0 Then Begin
                                 r := Rect;
                                 InflateRect(r, -1, -1);
                                 DBGrid1.Canvas.Lock;
                                 Try
                                  DBGrid1.Canvas.Pen.Color := clRed;
                                  DBGrid1.Canvas.Pen.Width := 2;
                                  DBGrid1.Canvas.MoveTo(r.Left, r.Top);
                                  DBGrid1.Canvas.LineTo(r.Right, r.Top);
                                 FInally
                                  DBGrid1.Canvas.Unlock;
                                 End;
                                 End;
end;

Everything works fine:

 

DBGrid_Unstyled_OK.PNG.34da0626bda8fa11700cb7925fb35fcd.PNG

 

Until the moment when I enable VCL Styles. At first, it appears okay:

 

DBGrid_Styled_OK.PNG.5660e2b8296fc25b3a7718acb7056ea9.PNG

 

But if you start to click (especially try to drag and drop column headers) things start to fall apart:

 

DBGrid_Styled_NOK.PNG.29703ad5d8e9e82e71b036ce8f668fc7.PNG

 

This does not seem to happen when there are no Styles enabled. Anyone has an idea on what is this and how to fix it?

 

Thanks!

Share this post


Link to post

Have you tried without the lock/unlock? You are after all already inside a grid on draw event.

A possible workaround could be to do an invalidate on the grid after a drag/drop.

 

 

Also - how a mod 2 = 0 can draw every three rows, is a bit of a mystery 😉

Share this post


Link to post

Yes, original code was without .Lock and .Unlock but since it started to glitch out I decided to include it (and it remained). As for the Mod 2 it is no mystery, only my mistake: took the shots before changing the value - was experimenting with a lot of things before asking.
Invalidating after a column drag will not be the solution - as this is only the sample code, and this is how I could recreate the issue. In the original project some lines are drawn correctly, some are in black (no matter that the color is set to red), some does not appear at all even without dragging involved.

Share this post


Link to post
 
 
 
 
 
 
 
 
 
 
8
17 minutes ago, aehimself said:

Yes, original code was without .Lock and .Unlock but since it started to glitch out I decided to include it (and it remained). As for the Mod 2 it is no mystery, only my mistake: took the shots before changing the value - was experimenting with a lot of things before asking.
Invalidating after a column drag will not be the solution - as this is only the sample code, and this is how I could recreate the issue. In the original project some lines are drawn correctly, some are in black (no matter that the color is set to red), some does not appear at all even without dragging involved.

What if  you call Inherited before drawing the rect?

I see that in some of my code I only do my custom drawing if State = [].

 

Share this post


Link to post

Soooo... the bad thing is that without major changes the original project started to work correctly and I don't know why!!!

 

Especially since the raw DBGrid seems to have this issue and I don't remember fixing anything in my descendant...

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×