gpt4 book ai didi

c# - 将 DataGridViewSelectedCellCollection 复制+粘贴到剪贴板或从剪贴板粘贴

转载 作者:太空宇宙 更新时间:2023-11-03 13:30:35 26 4
gpt4 key购买 nike

假设我有一个 DataGridView,它由 Cells 中的许多不同的 strings(不同的长度、数字和纯文本)填充。

我想做的就是复制粘贴这些字符串,可以是任意选择的Cells。

复制的方法是:

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];

}
}
}
}
}

效果很好除非字符串本身包含我用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

除了按定义的格式解析每个字符串之外,我还有其他选择吗?

最佳答案

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

在这种特定情况下,最简单的解决方案是将多行分隔符转换为 \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");

关于c# - 将 DataGridViewSelectedCellCollection 复制+粘贴到剪贴板或从剪贴板粘贴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20700950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com