Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

DBgrid title column caption

1,735 views
Skip to first unread message

toni

unread,
Jul 15, 2002, 2:48:56 AM7/15/02
to
HI!

I have a DBgrid that shows title caption in two rows (I am using D5).

example:

type
TAccessDBGrid = class(TDBGrid);

procedure Tform1.DBGrid3DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; column: TColumn; State: TGridDrawState);
var
S1, S2: String;
begin
with TAccessDBGrid(DBGrid3) do
begin
RowHeights[0] := 30;
Canvas.Brush.Style := bsClear;
case Column.Index of
0: begin
Column.Title.Caption := '';
S1 := '1.';
S2 := 'column';
end;
1: begin
Column.Title.Caption := '';
S1 := '2.';
S2 := 'column ';
end;
Canvas.TextOut(Rect.Left+2, 2, S1);
Canvas.TextOut(Rect.Left+2, 16, S2);
end;
end;


When I press key Home or End in Dbgrid, title captions for all columns
disappeare.
Why??. Moving again by one column captions get visible.
It is Dbgrid bug??

Please help me !


Alberto

unread,
Jul 15, 2002, 6:10:53 PM7/15/02
to
Instead of Making Title.Caption:='' make it Title.Caption=' ';

That will solve your problem. Seems like assigning empty caption to title it
takes Field.Name by default

Alberto

"toni" <antun....@sb.tel.hr> wrote in message news:3d327136_2@dnews...

toni

unread,
Jul 16, 2002, 1:24:31 AM7/16/02
to

Alberto <cha...@bellsouth.net> wrote in message news:3d33497e$1_1@dnews...


Thanks Alberto, but that didn't solve My problem.


Alberto

unread,
Jul 16, 2002, 1:11:47 PM7/16/02
to
It works for me. I modified your code like this

Actually if you use Caption:='' it will draw the FieldName plus S1 or S2


Alberto

"toni" <antun....@sb.tel.hr> wrote in message news:3d33aecf$1_1@dnews...

toni

unread,
Jul 17, 2002, 6:28:17 AM7/17/02
to

Alberto <cha...@bellsouth.net> wrote in message news:3d3454e3$1_1@dnews...
> > HI Alberto!


It works for me when dbgrid have 2-3 columns. If dbgrid have 10-15 columns
which are not in focus, and when I wont see
column nr.15 (press END key) , then title captions for all columns
disappeare.


Thanks in advance.


Alberto

unread,
Jul 17, 2002, 9:40:00 AM7/17/02
to
I am pretty sure something is wrong in your pc.

It works in my with 20 columns and use end and home and the Text assigned
are there.

There is no bug at all


Alberto
"toni" <antun....@sb.tel.hr> wrote in message news:3d354807_2@dnews...

Chris Luck

unread,
Jul 17, 2002, 11:57:56 PM7/17/02
to
"toni" <antun....@sb.tel.hr> wrote in message news:3d327136_2@dnews...
> I have a DBgrid that shows title caption in two rows (I am using D5).

> type


Hi, toni.

I see you are recycling some old and ugly code of mine.
It is, at best, an inefficient kludge.

> It is Dbgrid bug??

No. It is a limitation of the design, Borland never intended that you
should have drawing access to the Title row through code (other than for the
font, colour etc.).

The code for writing to the DBGrid Title area is located in a local
procedure in the DrawCell method, this renders it completely inaccessible to
code outside DrawCell and means you have no opportunity to selectively draw
in the Title area using the internal text drawing code.

The scheme behind the kludge is to use the DrawColumnCell event in such a
way that every time a cell is drawn it also draws over the Title area of the
same column if you have coded it to do so. However, it has to do this for
every cell in that column, so if you have 20 rows being drawn then the Title
cell is drawn 20 times. This is grossly inefficient but fortunately
full-grid draws are relatively few and with today's hardware you won't see
any obvious impact on performance in most cases.

> When I press key Home or End in Dbgrid, title captions for all columns
> disappeare.
> Why??.

In detail I cannot answer that but one influencing factor is the existence
or otherwise of the InplaceEditor, which is not created at the same time as
the grid itself but only emerges when you first go into edit mode. Once
created it persists until the grid is destroyed. My latest evolution of
this dreadful scheme is shown below and incorporates code to ensure that the
InplaceEditor is created on first showing. If the Titles still fail to draw
correctly try switching off DefaultDrawing and do all the drawing in the
DrawColumnCell event; it's easily done with just three lines of code.

As Alberto has found, you can set the relevant title captions to spaces
rather than empty strings and this is what I have done here, the particular
merit of doing so is that you can set these at design-time in the columns
editor (assuming you have persistent columns) thereby simplifying the
drawing code. You can simplify things further by hard-coding RowHeights
and by using the same font for the grid and titles. If you do intend to
have titles showing different colours or styles then stick with one name and
size of TitleFont and apply your changes in code, it's best not to mess with
the Column.Title.Fonts (leave them at their defaults).
By suitable juggling of casts and properties I have dispensed with the need
for the sub-class (TAccessDBGrid).

This concoction was brewed in D2 (I otherwise have BCB4), later
versions may well exhibit different behaviour, this probably explains why
Alberto is not getting the same behaviour as you are seeing. You will just
have to fiddle with it to see what you are able to get away with. Remember
this is a fragile attempt to avoid doing things the 'proper' way, i.e.
descend from TCustomDBGrid and write a fully functional grid as others
already have. There are various alternative grids out there, some free,
some not; or you could spend the odd weekend trying it yourself. :)


procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
S: String;
NewRect: TRect;
TestCanvas: TCanvas;
OldFont: TFont;
DC: HDC;

// -- Start Local procedure --
procedure WriteTitle;
begin
with TDBGrid(Sender) do
begin
Canvas.Brush.Style := bsClear;
OldFont := Canvas.Font;
Canvas.Font := TitleFont;
NewRect := Rect;
NewRect.Left := NewRect.Left +3;
NewRect.Top := 2;
DrawText(Canvas.Handle, PChar(S), -1, NewRect, DT_NOPREFIX or
DT_LEFT);
Canvas.Font := OldFont;
Canvas.Brush.Style := bsSolid;
end;
end;
// -- End Local procedure --

begin

with TDBGrid(Sender) do
begin
// Ensure that the InplaceEditor has been created
if ControlCount = 0 then
begin
EditorMode := true;
EditorMode := false;
end;

// Either hardcode RowHeights[0] or use the following
// section to set title height to accommodate 2 rows
// of text with the current TitleFont
// Note: Column.Title.Font is not the same as TitleFont

//------Set title height -------
DC := GetDC(0);
TestCanvas := TCanvas.Create;
try
TestCanvas.Handle := DC;
try
TestCanvas.Font := TitleFont;
TStringGrid(Sender).RowHeights[0] :=
DrawText(DC,
PChar('M'+#13#10+'M'),
-1,
NewRect,
DT_CALCRECT)
+ 5;
finally
ReleaseDC(0, DC);
end;
finally
TestCanvas.free;
end;
//------End title height -------

// include any other custom drawing here

// if DefaultDrawing is false in Object Inspector
// use these 3 lines to apply standard grid drawing
{ DefaultDrawColumnCell(Rect, DataCol, Column, State);
if gdFocused in State then
Canvas.DrawFocusRect(Rect); }

// if row-moving is disallowed use DataCol or Column.Index
// in a case statement, otherwise use if - then - else blocks

S := '';
if Column.FieldName = 'LabelName' then
S := 'Label'+#13#10+ 'Name'
else
if Column.FieldName = 'PaperHeight' then
S := 'Paper' + #13#10 + 'Height'
else
if Column.FieldName = 'PaperWidth' then
S := 'Paper' + #13#10 + 'Width';

if S <> '' then
WriteTitle;
end;
end;


Oh, and before I sign-off, a word about overquoting. You really don't need
to repeat the entire thread in your replies (especially not this part!).

--
Regards,
Chris Luck.


toni

unread,
Jul 18, 2002, 4:58:56 AM7/18/02
to
Thanks Chris, that solve My problem.

toni

unread,
Jul 18, 2002, 5:01:20 AM7/18/02
to
Thanks Alberto !


Chris Luck

unread,
Jul 18, 2002, 9:50:20 PM7/18/02
to
"toni" <antun....@sb.tel.hr> wrote in message news:3d3683f8$1_2@dnews...

> Thanks Chris, that solve My problem.

Great! It's good to know it's still useable.


--
Regards,
Chris Luck.


0 new messages