我已经看到Datagridview没有启用一个复制和粘贴多个单元格的文本,是否有一个简单的设置来启用它或我是否必须使用密钥处理程序和剪贴板数据存储来包括那个功能。
用户想要在一行中复制3个单元格,并且能够将它们的文本粘贴到不同的行中。
答案 0 :(得分:5)
好的,我得到了一个解决方案,但它还没有通过粘贴多行的单元格进行测试。这是datagridview的KeyDown事件
if (e.Control && e.KeyCode == Keys.C)
{
DataObject d = AccountGrid.GetClipboardContent();
Clipboard.SetDataObject(d);
e.Handled = true;
}
else if (e.Control && e.KeyCode == Keys.V)
{
string s = Clipboard.GetText();
string[] lines = s.Split('\n');
int row = AccountGrid.CurrentCell.RowIndex;
int col = AccountGrid.CurrentCell.ColumnIndex;
string[] cells = lines[0].Split('\t');
int cellsSelected = cells.Length;
for (int i = 0; i < cellsSelected; i++)
{
AccountGrid[col, row].Value = cells[i];
col++;
}
}
答案 1 :(得分:1)
string s = Clipboard.GetText();
string[] lines = s.Split('\n');
int row = dataGridView1.CurrentCell.RowIndex;
int col = dataGridView1.CurrentCell.ColumnIndex;
foreach (string line in lines)
{
string[] cells = line.Split('\t');
int cellsSelected = cells.Length;
if (row < dataGridView1.Rows.Count)
{
for (int i = 0; i < cellsSelected; i++)
{
if (col + i < dataGridView1.Columns.Count)
dataGridView1[col + i, row].Value = cells[i];
else
break;
}
row++;
}
else
{
break;
}
}
答案 2 :(得分:0)
if (e.Control && e.KeyCode == Keys.V)
{
string CopiedContent = Clipboard.GetText();
string[] Lines = CopiedContent.Split('\n');
int StartingRow = dataGridView1.CurrentCell.RowIndex;
int StartingColumn = dataGridView1.CurrentCell.ColumnIndex;
foreach (var line in Lines)
{
if (StartingRow <= (dataGridView1.Rows.Count - 1))
{
string[] cells = line.Split('\t');
int ColumnIndex = StartingColumn;
for (int i = 0; i < cells.Length && ColumnIndex <= (dataGridView1.Columns.Count - 1); i++)
{
dataGridView1[ColumnIndex++, StartingRow].Value = cells[i];
}
StartingRow++;
}
}}