Link to home
Start Free TrialLog in
Avatar of SheratonGroup
SheratonGroup

asked on

DBGrid Delphi Right Click with Popup Menu

     

I have a DBGrid component with a popup menu attached.  dgRowSelect and dgMultiSelect options are true, and need to remain this way.  

Can someone tell me how to make right click actually the row in a DBGrid before the popup menu appears?

Right clicking on the grid doesn't actually highlight the item right clicked upon, it selects the item and I believe also sets the .recno property of the associated dataset, but the row is not selected in the same way a left click selects rows.  

Basically I just want right click to highlight the row, just like left click does.  

The end behavior would be the same as doing the following, left click on a row to select, and then right click on the same row to bring up the popup menu.


Thanks,

sse
Avatar of aflarin
aflarin

try this OnMouseDown handler for DBGrid1:

type
  THackDBGrid = class(TDBGrid);

procedure TForm1.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  gc: TGridCoord;
begin
  if Button = mbRight then
  begin
    gc:= DBGrid1.MouseCoord(x, y);

    if (gc.X > 0) AND (gc.Y > 0) then
      DBGrid1.DataSource.DataSet.MoveBy(gc.Y - THackDBGrid(DBGrid1).Row);
  end;
end;

Open in new window

Avatar of Pierre Cornelius
Nice work aflarin. Simply, easy effective. You beat me to it...
Avatar of SheratonGroup

ASKER

Hi,

Thank you for that, but a protected hack is not what I am looking for.  

I was hoping for a simple win api call, eg, a sendmessage call that selects the right clicked row.  Unfortunately, I don't know the proper message to send the dbgrid to accomplish this.  

sse
The protected hack is just to access the row number. I wonder what brain-child thought this should be a protected property in the first place???

As for you sendmessage suggestion, there is no such support built into the dbgrid. It is not a windows control that delphi simply wraps around, it is a control written in delphi. In my mind the row number should have been a public property in the first place.
Just to add to my last comments:

the current cell is internally managed by the component. The only way to access it is using the Hack as suggested by aflarin or creating a descendant component and surfacing the property.
Dbgrid definitely handles and responds to sendmessages (and postmessages too), even though it does not wrap window controls.  

I say this because I have hacked dbgrid myself in the past, and written custom events that use sendmessage.  

Unfortunately, I can't hack dbGrid for this job (as much as I would like to).

Thank you anyway.
>>>Dbgrid definitely handles and responds to sendmessages (and postmessages too), even though it does not wrap window controls.  


I never said it does not. To clarify: I was talking about a message that give you the cell clicked or selecting the row of that cell.
Does anyone know what message I can send the dbGrid to change the selected row?
I believe the question was answered.
@SheratonGroup

Have you abandoned this question?
I have not abandoned the question, in fact I would welcome an answer.  It has not yet been answered.  

just so we're on the same page:

1. do you have rowselect option set?
2. do you have multiselect option set?
ASKER CERTIFIED SOLUTION
Avatar of Pierre Cornelius
Pierre Cornelius
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial