复制并粘贴DataGridView中的多个单元格 [英] Copy and paste multiple cells within DataGridView

查看:195
本文介绍了复制并粘贴DataGridView中的多个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到Datagridview不能启用一个复制和粘贴多个单元格的文本,是否有一个简单的设置来启用,或者我必须使用键处理程序和剪贴板数据存储来包括该功能。



用户想要复制一行中的3个单元格,并能够将它们的文本粘贴到另一行。


好的,我得到了一个解决方案,但没有通过粘贴多个行的单元格进行测试。这是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('\\\
');
int row = AccountGrid.CurrentCell.RowIndex;
int col = AccountGrid.CurrentCell.ColumnIndex;
string [] cells = lines [0] .Split('\t');
int cellsSelected = cells.Length; (int i = 0; i< cellsSelected; i ++)
{
AccountGrid [col,row] .Value = cells [i];
col ++;
}
}


I've seen that the Datagridview doesn't enable one to copy and paste the text of more than one cells, is there a simple setting to enable that or do I have to use the key handler and clipboard data store to include that functionality.

A user wants to copy 3 cells within a row and be able to paste the text of them in a different row.

解决方案

Ok, I got a solution but it hasn't been tested by pasting cells accross multiple rows.This is the KeyDown event of the datagridview

 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++;
                }
            }

这篇关于复制并粘贴DataGridView中的多个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆