K
K
KTG2016-12-29 10:06:36
Delphi
KTG, 2016-12-29 10:06:36

Delphi. DBGridEh. How to get the data of a cell when hovering over it with the mouse?

When I hover over a DBGridEh cell, I want to receive the record ID and that a button appears on the right side of the cell.
The button is the same for all entries, it is a TPanel in the invis with the OnClick event.
I move the mouse cursor over the cell, the panel moves to the right place, changes the Visible and Enable properties to true, in its click event I process the entry with the ID in the same cell. (The selected entry in DBgridEh does not change).
Found this way:

procedure TFSprav.DBGridEh2MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
 grCoord: TGridCoord;
 aGrid: TCrackDBGrid;
 NewActiveRecord, oldActiveRecord: integer;
 delta: string;
begin
  aGrid := TCrackDBGrid(Sender);
  grCoord := aGrid.MouseCoord(X, Y);
    aGrid.DataSource.DataSet.DisableControls;
      oldActiveRecord := aGrid.DataLink.ActiveRecord; // запоминаем текущую строку (отображаемую) не номер записи
      newActiveRecord := grCoord.Y - aGrid.DataLink.ActiveRecord; // получаем положение строки на которой наведена мышка.
    aGrid.DataLink.MoveBy(newActiveRecord);
    delta := aGrid.DataLink.DataSet.FieldByName('Name').AsString;
   if grCoord.X > 0 then
    begin
     caption := delta;
    end;
   aGrid.DataLink.ActiveRecord := oldActiveRecord; // возвращаем положение к изначальной строке
   aGrid.DataSource.DataSet.EnableControls;
end;

But a difficulty arose.
The DataSource has an OnDataChange event handler that passes the ID of the record selected in the grid to the parameters of another ADOQuery.
Separately, both methods work, but together they do not. The program gets stuck at the moment when MoveBy occurs on the DataSet when the cursor position changes, it triggers the OnDataChange event, which, when redrawing, apparently gets the mouse position again and calls MoveBy and so on in a circle until the mouse cursor is removed from the grid.
How to fix?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alex, 2016-12-29
@streetflush

Well, at the moment MoveBy
DBGridEh2.OnMouseMove := nil
and after manipulations
DBGridEh2.OnMouseMove := DBGridEh2MouseMove

K
Konstantin Tsvetkov, 2016-12-29
@tsklab

aGrid := TCrackDBGrid(Sender);
No need to break DBGridEh.
And you need to move not on the grid, but on the records, using Bookmark to fix the position and MoveBy to change it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question