DataGridView实现剪切,复制,粘贴

时间:2023-01-31 16:55:28

原文来自:http://hi.baidu.com/ctguyg/item/5c0d31095239f4016d904800

 

 DataGridView.ClipboardCopyMode 属性被设定为 DataGridViewClipboardCopyMode.Disable 以外的情况时,「Ctrl + C」 按下的时候,被选择的单元格的内容会拷贝到系统剪切板内。格式有: Text, UnicodeText,Html, CommaSeparatedValue。可以直接粘贴到 Excel 内。

       ClipboardCopyMode 还可以设定 Header部分是否拷贝: EnableAlwaysIncludeHeaderText 拷贝Header部分、EnableWithoutHeaderText 则不拷贝。默认是 EnableWithAutoHeaderText , Header 如果选择了的话,就拷贝。
1)编程方式实现剪切板的拷贝

Clipboard.SetDataObject(DataGridView1.GetClipboardContent())

2)编程方式实现剪切

Clipboard.SetDataObject(DataGridView1.GetClipboardContent())

然后清空DataGridView的单元格的内容


3) DataGridView 的数据粘贴

实现剪切板的拷贝比较容易,但是实现 DataGridView 的直接粘贴就比较难了。「Ctrl + V」按下进行粘贴时,DataGridView 没有提供方法,只能自己实现。

下面是我写的代码:

(1)复制

           Clipboard.SetDataObject(this.dataGridView1.GetClipboardContent());

(2)剪切

           Clipboard.SetDataObject(this.dataGridView1.GetClipboardContent());//先复制
            //然后将单元格的内容清空
            // 获取剪切板的内容,并按行分割  
            string pasteText = Clipboard.GetText();
            if (string.IsNullOrEmpty(pasteText))
                return;
            int colnum = 0;
            int rownum = 0;
            //获得当前剪贴板内容的行、列数
            for (int i = 0; i < pasteText.Length; i++)
            {
                if (pasteText.Substring(i, 1) == "\t")
                {
                    colnum++;
                }
                if (pasteText.Substring(i, 1) == "\n")
                {
                    rownum++;
                }
            }
            //粘贴板上的数据来自于EXCEL时,每行末都有\n,在DATAGRIDVIEW内复制时,最后一行末没有\n
            if (pasteText.Substring(pasteText.Length - 1, 1) == "\n")
            {
                rownum = rownum - 1;

            }
            colnum = colnum / (rownum + 1);

            //获取当前选中的最后一个单元格的位置
            string str = dataGridView1.SelectedCells.Count.ToString();
            int colindex = dataGridView1.SelectedCells[0].ColumnIndex;
            int rowindex = dataGridView1.SelectedCells[0].RowIndex;
            MessageBox.Show("列:" + colnum.ToString() + ",行:" + rownum.ToString() + "单元格:" + colindex.ToString() + "," + rowindex.ToString());
            for (int i = 0; i <= rownum; i++)
            {
                for (int j = 0; j <= colnum; j++)
                {
                    dataGridView1.Rows[rowindex - i].Cells[colindex - j].Value = "";
                }
            }

(3)粘贴

        private void DataGirdViewCellPaste()
        {
            try
            {

                // 获取剪切板的内容,并按行分割  
                string pasteText = Clipboard.GetText();
                if (string.IsNullOrEmpty(pasteText))
                    return;
                int tnum = 0;
                int nnum = 0;
                //获得当前剪贴板内容的行、列数
                for (int i = 0; i < pasteText.Length; i++)
                {
                    if (pasteText.Substring(i, 1) == "\t")
                    {
                        tnum++;
                    }
                    if (pasteText.Substring(i, 1) == "\n")
                    {
                        nnum++;
                    }
                }
                Object[,] data;
                //粘贴板上的数据来自于EXCEL时,每行末都有\n,在DATAGRIDVIEW内复制时,最后一行末没有\n
                if (pasteText.Substring(pasteText.Length - 1, 1) == "\n")
                {
                    nnum = nnum - 1;

                }

                tnum = tnum / (nnum + 1);
                data = new object[nnum + 1, tnum + 1];//定义一个二维数组

                String rowstr;
                rowstr = "";
                //MessageBox.Show(pasteText.IndexOf("B").ToString());
                //对数组赋值
                for (int i = 0; i < (nnum + 1); i++)
                {
                    for (int colIndex = 0; colIndex < (tnum + 1); colIndex++)
                    {
                        //一行中的最后一列
                        if (colIndex == tnum && pasteText.IndexOf("\r") != -1)
                        {
                            rowstr = pasteText.Substring(0, pasteText.IndexOf("\r"));
                        }
                        //最后一行的最后一列
                        if (colIndex == tnum && pasteText.IndexOf("\r") == -1)
                        {
                            rowstr = pasteText.Substring(0);
                        }
                        //其他行列
                        if (colIndex != tnum)
                        {
                            rowstr = pasteText.Substring(0, pasteText.IndexOf("\t"));
                            pasteText = pasteText.Substring(pasteText.IndexOf("\t") + 1);
                        }
                        data[i, colIndex] = rowstr;
                    }
                    //截取下一行数据
                    pasteText = pasteText.Substring(pasteText.IndexOf("\n") + 1);

                }
                //获取当前选中单元格所在的列序号
                int curntindex = dataGridView1.CurrentRow.Cells.IndexOf(dataGridView1.CurrentCell);
                //获取获取当前选中单元格所在的行序号
                int rowindex = dataGridView1.CurrentRow.Index;
                //MessageBox.Show(curntindex.ToString ());
                for (int j = 0; j < (nnum + 1); j++)
                {
                    for (int colIndex = 0; colIndex < (tnum + 1); colIndex++)
                    {
                        dataGridView1.Rows[j + rowindex].Cells[colIndex + curntindex].Value = data[j, colIndex];
                    }
                }
            }
            catch
            {
                MessageBox.Show("粘贴区域大小不一致");
                return;
            }
        }