KB Article A1266
Visible to All Users

How to implement a Copy/Paste feature

Description:
I need to cut/copy/paste cells and rows (like with MS Access). For example, copy an new row based on an existing one or copy data to/from Excel. Cell contents can be text, date, number, etc.

Answer:
The XtraGrid does not have the copy/paste feature built-in. However, it can be implemented via some additional coding. We advise that you review the following articles:
  Obtaining and Setting Cell Values in the XtraGrid help
  Placing Data on the Clipboard and Retrieving Information from the Clipboard in the MSDN Library
Some sample code for copying grid cell values to the clipboard can be found in the following articles:
  How to obtain the text of selected rows and copy it to the clipboard
  How to implement block selection for XtraGrid cells
To paste data from the clipboard to the grid, you should parse the clipboard's data and add new data rows directly into the grid's underlying data object. The new rows will automatically appear in the grid. We have attached a sample project.

Show previous comments (4)

    Is there a sample project for paste? The actual text in Nick's message doesn't make sense: "To paste data from the clipboard to the grid, you should parse the clipboard's data and add new data rows directly into the grid's underlying data object."
    The objection is " you should parse the clipboard's data and…" – what does that mean? I thought the point was that we can't actually paste the data. So, to WHAT is the data pasted, before adding new data rows, as Nick has stated?

      I'll also add the question about whether there is any event to hook to know that a paste is being attempted? Again, we really need this exact thing to be sample coded. A Google search shows a number of people asking this same question here, and more on other programming sites, specifically about DevEx's grid. Surely with such a common task, it should be part of your framework.

      DevExpress Support Team 10 years ago

        Hello Richard,
        To review how the 'Paste' feature works, please refer to the attached code example: How to implement the Copy/Paste feature. It demonstrates one of many possibilities of this feature implementation. As for providing this functionality out-of-the-box, I'm afraid that it's impossible to take into account all possible scenarios. To paste data into a grid, it's necessary to pass this data to the underlying data source that can be represented in different ways. For example, if it is a DataTable, as in the example mentioned above, it is pretty easy to parse data from the clipboard and pass it to a data source. If the data source is a list of custom objects, you need to determine what property values you've just obtained and assign correct values to correct properties. Thus, the task becomes more complex, and its implementation depends on requirements in a particular case.
        As for your question about catching the moment when data is about to be pasted, it's better to process this at the UI level. GridControl is not aware of how its data source was modified. Thus, if you wish to paste data on a button click / via a keyboard shortcut, handle the corresponding Click / KeyDown event.
        Please let us know if you have difficulties with this task. You can describe your scenario, and we'll do our best to find an appropriate solution.
        I'm looking forward to your reply.

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.