复制+粘贴DataGridViewSelectedCellCollection到/从剪贴板

时间:2013-12-20 10:00:33

标签: c# datagridview clipboard

假设我有一个DataGridView,其中包含多个不同的strings(不同的长度,数字和纯文本)。

我想要做的就是复制并粘贴这些字符串,这些字符串可以是任何选择的单元格。

复制的方法是:

if (e.Control && e.KeyCode == Keys.C)
{
    // copy
    DataGridViewSelectedCellCollection tmpCells = this.MyDataGridView.SelectedCells;
    Clipboard.SetDataObject(tmpCells);
 }

哪种方法正常。

粘贴的方法是:

if (e.Control && 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 < rowsInClipboard.Length; ++iRow )
    {
        string[] valuesInRow = rowsInClipboard[iRow].Split(columnSplitter);

        for (int iCol = 0; iCol < valuesInRow.Length; ++iCol )
        {
            if (this.MyDataGridView.ColumnCount-1 >= c + iCol)
            {

                DataGridViewCell DGVC = (this.MyDataGridView.Rows[r + iRow].Cells[c + iCol]);

                DGVC.Value = valuesInRow[iCol];

             }
          }
     }
  }
}

哪种方法正常 UNLESS 字符串本身不包含包含我使用rowSplittercolumnSplitter指定的任何分隔符。但不幸的是,这种情况经常发生。然后它将字符串分开并将其扩展到下一个单元格。

示例:

Cell[n] = {"This string contains a new line delimiter \n but should use only one cell."}

将粘贴到:

Cell[n] = {"This string contains a new line delimiter"}; 
Cell[n+1] = {"but should use only one cell."}

所以我的问题是:是否有可能恢复之前复制到剪贴板的DataGridViewSelectedCellCollection?只需从object转换为DataGridViewSelectedCellCollection即可生效:

DataGridViewSelectedCellCollection DGSCC = (DataGridViewSelectedCellCollection)dataInClipboard; // compiles, but throws exception at runtime

我是否还有其他选项,但是按照定义的格式解析每个字符串?

1 个答案:

答案 0 :(得分:1)

您必须为剪贴板定义自己的格式,这将执行默认情况下无法为您执行的操作。

在这种特定情况下最简单的解决方案是将多行中断转换为\n,然后在粘贴时转换回来,但无论如何它不再意味着

DataGridViewSelectedCellCollection tmpCells = this.MyDataGridView.SelectedCells;
Clipboard.SetDataObject(tmpCells);

DataGridViewSelectedCellCollection tmpCells = this.MyDataGridView.SelectedCells;
string result = "";
foreach(DataGridViewCell cell in tempCells)
    // ... try to replicate what default clipboard text representation does
// change line breaks
Clipboard.SetDataObject(result.Replace("\xd\xa", "\\n"));

粘贴将是:

IDataObject dataInClipboard = Clipboard.GetDataObject();
string stringInClipboard = dataInClipboard.GetData(DataFormats.Text).ToString().Replace("\\n", "\xd\xa");