Note that there are some explanatory texts on larger screens.

plurals
  1. POCopy+Paste DataGridViewSelectedCellCollection to/from Clipboard
    primarykey
    data
    text
    <p>Assume that I have a <code>DataGridView</code> which is populated by a number of different <code>strings</code> (different length, numbers and plain text) in Cells.</p> <p>Want I want to do is to copy and paste these strings, which could be any selection of Cells.</p> <p>My approach to <strong>copy</strong> is:</p> <pre><code>if (e.Control &amp;&amp; e.KeyCode == Keys.C) { // copy DataGridViewSelectedCellCollection tmpCells = this.MyDataGridView.SelectedCells; Clipboard.SetDataObject(tmpCells); } </code></pre> <p>Which is working properly.</p> <p>My approach to <strong>paste</strong> is:</p> <pre><code>if (e.Control &amp;&amp; e.KeyCode == Keys.V) { // paste IDataObject dataInClipboard = Clipboard.GetDataObject(); string stringInClipboard = (string)dataInClipboard.GetData(DataFormats.Text); char[] rowSplitter = { '\r', '\n' }; char[] columnSplitter = { '\t' }; string[] rowsInClipboard = stringInClipboard.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries); int r1 = this.MyDataGridView.SelectedCells[0].RowIndex; int c1 = this.MyDataGridView.SelectedCells[0].ColumnIndex; int r2 = this.MyDataGridView.SelectedCells[this.MyDataGridView.SelectedCells.Count-1].RowIndex; int c2 = this.MyDataGridView.SelectedCells[this.MyDataGridView.SelectedCells.Count-1].ColumnIndex; int r = Math.Min(r1, r2); // Do not care if selection was taken by drag mouse up or down, always start from min int c = Math.Min(c1, c2); // Do not care if selection was taken by drag mouse left or right, always start from min for (int iRow = 0; iRow &lt; rowsInClipboard.Length; ++iRow ) { string[] valuesInRow = rowsInClipboard[iRow].Split(columnSplitter); for (int iCol = 0; iCol &lt; valuesInRow.Length; ++iCol ) { if (this.MyDataGridView.ColumnCount-1 &gt;= c + iCol) { DataGridViewCell DGVC = (this.MyDataGridView.Rows[r + iRow].Cells[c + iCol]); DGVC.Value = valuesInRow[iCol]; } } } } } </code></pre> <p>Which works fine <strong>UNLESS</strong> the string itself <strong>DOES NOT</strong> contain any delimiter I specified with <code>rowSplitter</code> and <code>columnSplitter</code>. But this unfortunately is the case very often. It then separates the string and expands it to the next cell.</p> <p><strong>Example:</strong></p> <pre><code>Cell[n] = {"This string contains a new line delimiter \n but should use only one cell."} </code></pre> <p>Will be pasted to: </p> <pre><code>Cell[n] = {"This string contains a new line delimiter"}; Cell[n+1] = {"but should use only one cell."} </code></pre> <p>So my question is: is it possible to restore the <code>DataGridViewSelectedCellCollection</code> as it was copied to the clipboard before? Just casting from <code>object</code> to <code>DataGridViewSelectedCellCollection</code> will not work:</p> <pre><code>DataGridViewSelectedCellCollection DGSCC = (DataGridViewSelectedCellCollection)dataInClipboard; // compiles, but throws exception at runtime </code></pre> <p>Do I have any other option but parsing each string by a defined formatting?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload