I need to implement Cut/Copy/Paste functionality within an application, but not for use with Excel - it is in the application only.
I'm handling the BeforePerformAction event and handling the cut/copy/paste actions, and serializing my custom type to/from the clipboard manually. The event is firing as expected, and the clipboard operations work correctly (outside of the grid). However, when I put the two together, it doesn't seem to be working quite right.
I first tried cancelling the action after copying to the clipboard, but if I do that, then the paste action never fires. If I don't cancel the action after copying, then it looks like the grid is putting it's own data into the clipboard (haven't had a chance to actually look at the data here yet though). Is there some better way to implement this? Should I just handle the keypresses directly? -- though if I do this, I may have problems later with toolbar menu items.
Hi Vineas,
If you cancel the BeforePerformAction, then the grid will not post anything to the clipboard. You can, of course, post your own data to the clipboard, but you obviously can't expect the grid to recognize your custom data format. If you paste into the grid at this point, I would think that the BeforePerformAction would still fire, but it sounds like you are saying it does not. I guess the grid must be examining the contents of the clipboardbefore firing the BeforePerformAction and determining that there is nothing on the clipboard that the grid can recognize. This seems a little odd. One might argue that the grid should fire the event first before checking the clipboard contents. But then the event would fire when no action is really being performed, so it's a little odd either way. Changing the firing of the event now would break existing applications, so it's not something that can be changed at this point.
On the other hand, if you don't cancel the BeforePerformAction, then the write some data to the clipboard, the grid will then go ahead and write it's data to the clipboard after you are done. The Clipboard object can store data in multiple formats, though. So you might be able to specify a format for you data that the grid does not use. I'm not sure this will work. The grid might be clearing the contents of the clipboard before it starts writing, but if that's the case, I think that would be a bug.
Another potential solution would be to use a different event. Perhaps you can use BeforeMultiCellOperation, instead. This event might have the same issues, though, I'm not sure.
You definitely described the behavior I'm seeing, and it definitely looks like the grid is checking out the clipboard's contents before a paste - using reflector, before an action is performed, there is a check to see if the action is allowed. This is where the paste is being killed. Unfortunately, this is the spot that is stopping my code from working. I'm going to start looking into a way to override this behavior - which looks like it may be possible since the KeyActionMappings property is public (any pointers in this area would be appreciated).
After finding and using a clipboard viewer program, I don't think it's the case that the grid is clearing the clipboard before writing anything - it's just that the grid writes in so many formats that it may as well do just that. As far as I can tell, the grid writes in any format that the custom type could be written in. As far as the BeforeMultiCellOperation event is concerned - it is raised after the BeforePerformAction, so ends up having the same problems - probably as a simple side-effect of when it is raised.
After messing around with it some more, and doing much additional searching in Reflector, it's looking less and less like I can do this. Here's what I've done:
I went through the list of GridKeyActionMappings to find the Paste action. I then created a new action mapping based off of the existing that doesn't require the state of UltraGridState.CanPaste (which is only set if the clipboard contains data that the grid recognizes), and removed the existing one from the list. I was thinking that since my action says the paste can go through no matter what the state, then it'll come through, and I'll handle it myself. It didn't work. I tried calling the DoesMappingApply() method to see if it should work, and this method returns true, as expected. I then tried calling the IsActionAllowed() method on the KeyActionMappings property on the grid; it returned false, contrary to what DoesMappingApply() returned.
Digging in reflector, I found out that IsActionAllowed() ends up looking into a different list than what is publicized - and it (of course) is making sure the CanPaste state is required for that action. So, even though my action doesn't require the CanPaste, it is still not allowed. There is no access to this list. It is a list that is only created internally, using protected classes, and there is no way to access it outside of reflection.
I'm back to the beginning again, only much more frustrated then before, and apparently with fewer (no?) options remaining. Where should I go from here?
It's been a while since I worked with the clipboard in any great detail, but isn't it possible for you to create your own custom clipboard format that the grid can't possibly be using? Maybe I am wrong, but I thought you could specify your own custom format as a string. That would allow you to leave the grid functionality as-is and still have your own data on the clipboard separate from the grid.
If not, then my only other recommendation at this point would be to Submit an incident to Infragistics Developer Support.
You can include a link to this post and ask them to write up the issue as a bug for developer review. This may be a feature request, but it seems like there ought to be some easy way that we could make it possible to do what you want here.