-1

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 ?

user763539
  • 3,509
  • 6
  • 44
  • 103
  • Are you in Bound Mode, Unbound Mode, Provider Mode? If unbound, have a look at cxGridView.DataController and there's a Values property I think which will let you assign the value to a particular cell – Jason Mar 11 '14 at 04:51
  • Does not matter in which mode.I need to paste the caption of the popupmenu into the cell. – user763539 Mar 11 '14 at 05:08
  • Yes, it does matter. The mode will dictate the method. Bound mode you'd use your dataset, Unbound - as I mentioned - you could use the Values property - or use whatever method you're currently using to populate the grid – Jason Mar 11 '14 at 05:15
  • It's unbound... So how do I paste the value? – user763539 Mar 11 '14 at 05:16
  • If your captions contain any escaped ampersands, they'll be removed. Use Menus.StripHotKey. – David Heffernan Mar 11 '14 at 06:57

2 Answers2

0

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.

Guillem Vicens
  • 3,936
  • 30
  • 44
  • Can you please ellaborate on what does not work? Do both events get fired? Do `fItem` and `FRec` get its values? Does the `OnClick` get fired? Also be careful with the `OnClick`as you must assign it to the `menu items` of the `popup menu`, **not** the `OnMenuItemClick` of your `TcxGridPopupMenu`. I tested this on `Delphi XE5` with `DevExpress VCL 13.2`. – Guillem Vicens Mar 11 '14 at 14:56
  • Nothing happens when I click on the menuitem. Underlying cell remains empty. – user763539 Mar 12 '14 at 06:08
  • "Nothing happens" is too vague. Please consider answering the questions on my previous comment or I will not be able to help you. – Guillem Vicens Mar 12 '14 at 06:59
  • Nothing gets pasted....pure and simple....The suggestion from Uli works but only on the first menuitem. – user763539 Mar 13 '14 at 06:12
  • You are describing the final effect, and not answering any of the questions I posted in my prior comment. Those questions can be answered if you debug the process. Without getting the answers to those questions I can not help you further. – Guillem Vicens Mar 13 '14 at 07:20
0

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;
Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83