Советы по Delphi

         

Получение текста ячейки TDBGrid под курсором мыши


    type TDBGridHack = class(TCustomDBGrid);
...................
function
GrabDbGridCellText(AGrid : TDBGrid; X, Y : Integer) : string;
var Pt : TGridCoord; CurrentRec : Integer; HasTitles, HasIndicator : boolean;
begin
HasTitles := dgTitles in AGrid.Options; HasIndicator := dgIndicator in AGrid.Options; Pt := AGrid.MouseCoord(X, Y); if AGrid.DataSource.DataSet.Active and (Pt.Y >= 0) and not ((Pt.Y = 0) and HasTitles) and not ((Pt.X = 0) and HasIndicator) then begin

CurrentRec := TDbGridHack(AGrid).DataLink.ActiveRecord; if HasTitles then Pt.Y := Pt.Y - 1; if HasIndicator then Pt.X := Pt.X - 1; TDbGridHack(AGrid).DataLink.ActiveRecord := Pt.Y; try Result := AGrid.Columns[Pt.X].Field.AsString; finally TDbGridHack(AGrid).DataLink.ActiveRecord := CurrentRec; end; end else Result := ''; end;

Пример вызова:

    procedure TForm1.RxDBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
Label1.Caption:=GrabDbGridCellText(RxDBGrid1,x,y); end;

Проверено только на Delphi 5. [001947]



Содержание раздела