2010-08-05 12 views

Répondre

2

Tofig, vous pouvez utiliser la procédure MouseCoord pour obtenir la ligne actuelle et col, mais pour montrer la valeur des pos [Col,Row] vous devez définir la propriété DataLink.ActiveRecord à la valeur de ligne et créer un nouveau descendant de la classe pour accéder à la propriété protégée .

vérifierons ce code

type 
THackGrid = class(TCustomDBGrid); //Create a new class to access the protected properties 


procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X, 
    Y: Integer); 
var 
    Cell    : TGridCoord; 
    Row,Col   : integer; 
    OrigActiveRecord : integer; 
begin 
    inherited; 
    Cell:=DBGrid1.MouseCoord(X,Y); 
    Col:= Cell.X; 
    Row:= Cell.Y; 

    if dgTitles in DBGrid1.Options then Dec(Row); //if the titles are shown then adjust Row index (-1); 
    if dgIndicator in DBGrid1.Options then Dec(Col); //if the indicator is shown then adjust the Column index (-1); 

    if THackGrid(DBGrid1).DataLink.Active and (Row>=0) and (Col>=0) then 
    begin 
     OrigActiveRecord:=THackGrid(DBGrid1).DataLink.ActiveRecord; //save the original index 
     try 
     THackGrid(DBGrid1).DataLink.ActiveRecord:= Row; 
     Label1.Caption:=DBGrid1.Columns[Col].Field.AsString; //show the current value in a tlabel 
     finally 
     THackGrid(DBGrid1).DataLink.ActiveRecord:= OrigActiveRecord; //restore the index 
     end; 
    end; 
end; 
+0

Vous n'avez pas besoin d'obtenir MouseCoord. L'événement OnMouseMove les a déjà. – Linas

+0

@Linas, les valeurs X et Y retournent un point (X, Y), vous avez besoin de la fonction 'MouseCoord' pour convertir ces valeurs aux index col et row, vérifiez ce lien http://docwiki.embarcadero.com/VCL /fr/Grids.TCustomGrid.MouseCoord – RRUZ

+0

Merci, cela a fonctionné. J'ai cependant le sentiment qu'il devrait y avoir un moyen plus facile. Je veux dire que DbGrid devrait stocker ces valeurs quelque part, afin de les peindre. Ne pouvons-nous pas simplement y accéder directement? –

Questions connexes