Как определить значение ячейки DbGrid во время события OnMouseMove - PullRequest
0 голосов
/ 05 августа 2010

У меня есть событие OnMouseMove, во время которого я хочу найти значение определенной ячейки (не обязательно той, которая находится под мышью).В основном вопрос заключается в следующем: как получить доступ к данным ячейки, используя ее координаты x и y, не выбирая их, не меняя фокус и т. Д.

1 Ответ

2 голосов
/ 05 августа 2010

То есть, вы можете использовать процедуру MouseCoord для получения текущей строки и столбца, но для отображения значения позиции [Col,Row] необходимо установить для свойства DataLink.ActiveRecord значение строки.и создайте новый потомок класса для доступа к защищенному свойству.

проверьте этот код

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;
...