KB Article A332
Visible to All Users

How to obtain the text of selected rows and copy it to the clipboard

Description:
How to obtain the text of selected rows and copy it to the clipboard?

Answer:
General concepts for processing selected rows are described in the topic in the XtraGrid help. This article demonstrates how to iterate selected rows and retrieve values from cells.
Below is a function, which returns a string composed from cell values. There are two nested loops to iterate through selected rows and columns within a row. The SelectedRowsCount and GetSelectedRows members of the GridView class are used in the outer loop. Columns are traversed via the VisibleColumns collection property. The GetRowCellDisplayText method is used to obtain the display values from the cells. You may use the GetRowCellValue method instead to retrieve "pure" values.
You should use the Clipboard.SetDataObject method to copy data to the clipboard. Please use Clipboard.GetDataObject for pasting. The attached project demonstrates the use of these methods for copying cell values and pasting them into a memo editor. Additional information on working with the clipboard in .NET Framework can be found in the MSDN Library.

C#
private string GetSelectedValues(GridView view) { if(view.SelectedRowsCount == 0) return ""; const string CellDelimiter = "\t"; const string LineDelimiter = "\r\n"; string result = ""; // iterate cells and compose a tab delimited string of cell values for(int i = view.SelectedRowsCount - 1; i >= 0; i--) { int row = view.GetSelectedRows()[i]; for(int j = 0; j < view.VisibleColumns.Count; j++) { result += view.GetRowCellDisplayText(row, view.VisibleColumns[j]); if(j != view.VisibleColumns.Count - 1) result += CellDelimiter; } if(i != 0) result += LineDelimiter; } return result; }
Visual Basic
Private Function GetSelectedValues(ByVal View As GridView) As String If View.SelectedRowsCount = 0 Then Return "" Const CellDelimiter As String = vbTab Const LineDelimiter As String = vbCrLf Dim Result As String = "" ' iterate cells and compose a tab delimited string of cell values Dim I, J As Integer Dim Row As Integer For I = View.SelectedRowsCount - 1 To 0 Step -1 Row = View.GetSelectedRows()(I) For J = 0 To View.VisibleColumns.Count - 1 Result += View.GetRowCellDisplayText(Row, View.VisibleColumns(J)) If J <> View.VisibleColumns.Count - 1 Then Result += CellDelimiter End If Next If I <> 0 Then Result += LineDelimiter End If Next Return Result End Function

See Also:
How to implement block selection for XtraGrid cells
How to implement a Copy/Paste feature

Show previous comments (1)
Alisher (DevExpress Support) 11 years ago

    Hi Mark,
    You can use the GridView.GetSelectedCells method which returns the selected cells. Check the documentation article related to this method and let me know if you have any questions.

      As an alternative to GridView.GetSelectedCells, is it possible to use SendKeys.Send("^©") to quickly copy a cell selection to the clipboard, in exactly the same way as pressing ctrl+c does on a gridview? I tried this but couldn't get it to work so just not sure if I was doing something wrong.
      Thanks

      DevExpress Support Team 11 years ago

        Hello Tristan,
        To avoid discussing multiple topics in this thread, I have extracted your original inquiry to a separate ticket created on your behalf: T130143: How to obtain the text of selected rows and copy it to the clipboard. For quick and efficient responses, we kindly request that future tickets cover one issue at a time. Your time and cooperation are appreciated.

        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.