Autor Beitrag
uli
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41



BeitragVerfasst: Do 26.09.02 10:22 
Hallo Leute

Ist es möglich im DrawColumnCell-Ereignis vom DBGrid festzustellen, welcher Datensatz (Datensatznummer) gerade in's Grid geschrieben wird?
Hintergrund meiner Frage ist, ich möchte jeden zweiten Datensatz farbig hinterlegen. Ich müsste also abfragen, ob die aktuelle Datensatznummer durch 2 teilbar ist und dann Brush.Color auf eine Farbe setzen.
Mit der Abfrage über Tabelle.RecNo hat es nicht so richtig funktioniert.

Hat jemand vielleicht eine Idee?

Gruß Uli
Lemmy
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 792
Erhaltene Danke: 49

Windows 7 / 10; CentOS 7; LinuxMint
Delphi 7-XE10.1, VS 2015
BeitragVerfasst: Do 26.09.02 12:09 
Hi,

hier der komplette Code:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure TfrmAdresse.DBGridKundeDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
var dbGrd:TDBGrid;
begin
  dbGrd:=TDBGrid(Sender);
  if gdSelected in State then
  begin
    dbGrd.Canvas.Brush.Color := $00DDDDFF;
    dbGrd.Canvas.Font.Color := clBlack;
  end  {need that highlight for 'focus'}
  else
 if (DMAdresse.DSKundeGrid.DataSet.RecNo mod 2) = 0 then
  begin
    dbGrd.Canvas.Font.Color := clBlack;
    dbGrd.Canvas.Brush.Color := $00FFBBBB  //clSilver;
  end;
  dbGrd.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;


Das hier:
if (DMAdresse.DSKundeGrid.DataSet.RecNo mod 2) = 0 then
suchst Du...

Grüße
Lemmy
uli Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41



BeitragVerfasst: Do 26.09.02 13:12 
So funktiniert es.
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure THauptForm.DBGrid5DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
begin
     if KADaoTable14.RecNo mod 2 =0 then
        DBGrid5.Canvas.Brush.Color:=12648324
     else
         DBGrid5.Canvas.Brush.Color:=clWhite;
     DBGrid5.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

Es geht wenn ich die RecNo von der Tabelle abfrage.
Mein Fehler war, ich habe statt DBGrid5 immer DBGrid1 geschrieben, da kann ich lange warten, dass was passiert.
Trotzdem danke für die schnelle Antwort.

Gruß Uli
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Fr 11.10.02 15:25 
Titel: Vorsicht bei SQL-Abfragen
Grundsätzlich gibt es bei den Lösungen mit RecNo nichts einzuwenden. Wenn Du allerdings Deine Datenbankabfragen mit TQuery (also SQL) durchführst, kann es passieren, dass die aktuelle Datensatznummer immer "0" ist.
Das hängt mit der Ergebnismenge bei SQL zusammen, die oft nur einen Teil der Datensätze holt und dann nicht mehr die Datensatznummern ermitteln kann.
Falls Du also Deine Idee in einer neuen DBGrid-Komponente einsetzen willst, wird es nicht in jedem Fall funktionieren.

Gruß Peter