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 BasicPrivate 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
Thank you.
This code copies entire rows. Not just the selected cells. If I only what the selected cells (adjacent selection) what would be the method to use.
Thanks
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
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.