Question

IN TMS string grid I used to use this to paste the caption of the popup menu into the grid's cell :

var
  s:string;
begin
  s:=(Sender as TmenuItem).Caption;
  s:=stringReplace(s,'&','',[rfReplaceAll]);
  with AdvStringGrid1 do
    Cells[Col,Row]:=s;

I never used this before in a cxGrid so I am totally new to this. I have linked cxGridpopUpMenu1 to my grid,added a classic PopUpMenu so it gets used by the cxGridpopUpMenu1,added some items in the popup menu and thats it. popup menu fires on right click in the grid ok, but how do you paste the value of the menuitem into the cell?? + Is there a way to assign popopmenu to a particular column ?

Was it helpful?

Solution

I'd do it like this:

procedure TForm1.MenuItem1Click(Sender: TObject);
var
  s: string;
begin
  Assert(Sender is TMenuItem);
  s := StripHotKey(TMenuItem(Sender).Caption);
  cxGrid1TableView1.DataController.Edit;
  cxGrid1TableView1.Controller.FocusedColumn.EditValue := s;
end;

OTHER TIPS

This can be done combining two event handlers:

  • The OnPopUp handler of your TcxGridPopupMenu.
  • An OnClick handler for all your popup menu items.

The idea is to use the OnPopup to store a reference to the item (column) and record clicked, while the OnClick would apply the value to the cell.

Code is as following:

//in private section of your form
fItem: TcxCustomGridTableItem; 
fRec: TcxCustomGridRecord;

procedure TForm1.cxGridPopupMenu1Popup(ASenderMenu: TComponent;
  AHitTest: TcxCustomGridHitTest; X, Y: Integer; var AllowPopup: Boolean);
begin
  if AHitTest is TcxGridRecordCellHitTest then
  begin
    fItem := TcxGridRecordCellHitTest(AHitTest).Item;
    fRec := TcxGridRecordCellHitTest(AHitTest).GridRecord;
  end;
end;

procedure TForm1.MenuItem1Click(Sender: TObject);
var
  s : string;
begin
  s := (sender as tmenuItem).Caption;
  gridView.DataController.Values[frec.Index, fitem.Index] := StripHotKey(s);
end;

As @DavidHeffernan suggested, notice the use of StripHotKey that removes the accelerator character mark from the menu caption.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top