How owner draw works:
Every time your StringGrid is filled, scrolle, recalculated, the pixels of the control are re-drawn.
This means several things:
- it will not work at design time
- if you color a cell, the content is "gone" (overdrawn), so you have to draw it again as well.
- there is a property, you have to set to true, that it works at all, I think it was "ownerdrawn" or so.
You are discouraged now?
Do not worry, here is a method for DBGrid, you can adjust.
Just search this property, set it to true.
And then click the method and paste the code below into it.
Comment those lines, which will not work for TStringGrid (because they are for TDBGrid).
It does it all
- comment how to draw where
- re-draw the fonts
- calculate some things
- many things you will not need (everything with query), just delete them.
Enjoy
procedure TFrame_MeinTag.DBGrid_einTagDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var farbe: TColor;
s: string;
begin
// wird jede Zelle aufgerufen, dabei liegen dieParameter so:
/// IBQuery_Alltag.RecNo 1 1 1 1
/// 2 2 2 2
/// column.index 0 1 2 3
/// 0 1 2 3
/// 0 1 2 3
/// columns 0 1 2 3
/// 0 1 2 3
/// 0 1 2 3
/// just draw here, no content change
with (Sender as TDBGrid) do begin
if DataCol=0 then begin // das soll nicht JEDE Spalte getan werden
if DataSource_EinTag.DataSet.RecNo=1
then comm_temp:= 'keine';
s:=DataSource_EinTag.DataSet.FindField('COMM').AsAnsiString;
s:=Trim(s);
faerbe_zeile:=9999;
if s <> comm_temp then begin // sucht, ob eine neue Ware in einer Zeile
comm_temp:=s;
faerbe_zeile:=ADQuery_einTag.RecNo; // diese Zeile wird gefärbt
end; end; // Ende der Waren-Suchschleife DataCol=0
// jetzt kommt das Färben selbst
if faerbe_zeile=ADQuery_einTag.RecNo
then farbe := $00E7F2FF // cremefarben, eine Zeile
else farbe := clWhite;
if (gdSelected in State) then Canvas.Brush.Color := clblue;
// das färbt jetzt nicht mehr
// einmal belassen, die Cremefarbe ist wichtiger
Canvas.Brush.Color := farbe; // Font-Farbe immer schwarz
Canvas.Font.Color := clBlack;
Canvas.FillRect(Rect); // Rechteck malen und Text schreiben
if Column.Field <> nil then begin
s:=Column.Field.AsString;
Canvas.TextOut(Rect.Left + 2, Rect.Top + 1,s);
end;
// TextRect(Rect: TRect; X, Y: Integer; const Text: string);
// wäre eine Alternative, allerdings wird der Text abgeschnitten, wenn er zu lange
// If (Datacol = 18) then // geht nicht für 17
// Canvas.TextOut(Rect.Left + 2, Rect.Top + 1,Column.Field.AsString);
// theoretisch ginge auch das oben, aber nur für nicht calc-Felder
end; // zu (Sender as TDBGrid)
end;