DataGridview中的行复制/粘贴功能(Windows应用程序) [英] Row copy/paste functionality in DataGridview(windows application)

查看:178
本文介绍了DataGridview中的行复制/粘贴功能(Windows应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#Windows应用程序,并希望在DataGridView中复制一行并将其粘贴到一个新行中。我该如何实现呢?我正在使用.net框架3.5。

I'm working on a C# windows application, and would like to copy a row in a DataGridView and paste it into a new row. How I can achieve this? I am using .net framework 3.5.

你能给我一些想法或一些代码,表明我能如何实现这一点吗?

Can you please provide me with some ideas or some code that would indicate how I could achieve this?

推荐答案

我找到了一个 post ,其中包含将剪贴板中的值粘贴到DataGridView中的代码。

I have found a post that contains code to paste values from clipboard into DataGridView.


我是googling如何粘贴到
DataGridView在C#从剪贴板,
信息,从Excel复制,没有
找到完整的答案。收集了来自论坛的
线程,并提出了
这个答案,希望它会使
的生活更轻松。你不必
了解代码只是复制和
粘贴

I was googling how to paste to DataGridView in C# from clipboard, an info, copied from Excel, and didn't find full answer. Collected couple of threads from forums and came up with this answer, hope it will make someones life easier. You dont have to understand the code just copy and paste

下面是一个修改版本。除了小的重构,我禁止粘贴到ReadOnly单元格。

Below is a bit modified version. Beyond small refactoring I forbid paste into ReadOnly cells.

使用示例:

private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
{
    ClipboardUtils.OnDataGridViewPaste(sender, e);
}

代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Commons
{
    public class ClipboardUtils
    {
        public static void OnDataGridViewPaste(object grid, KeyEventArgs e)
        {
            if ((e.Shift && e.KeyCode == Keys.Insert) || (e.Control && e.KeyCode == Keys.V))
            {
                PasteTSV((DataGridView)grid);
            }
        }

        public static void PasteTSV(DataGridView grid)
        {
            char[] rowSplitter = { '\r', '\n' };
            char[] columnSplitter = { '\t' };

            // Get the text from clipboard
            IDataObject dataInClipboard = Clipboard.GetDataObject();
            string stringInClipboard = (string)dataInClipboard.GetData(DataFormats.Text);

            // Split it into lines
            string[] rowsInClipboard = stringInClipboard.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries);

            // Get the row and column of selected cell in grid
            int r = grid.SelectedCells[0].RowIndex;
            int c = grid.SelectedCells[0].ColumnIndex;

            // Add rows into grid to fit clipboard lines
            if (grid.Rows.Count < (r + rowsInClipboard.Length))
            {
                grid.Rows.Add(r + rowsInClipboard.Length - grid.Rows.Count);
            }

            // Loop through the lines, split them into cells and place the values in the corresponding cell.
            for (int iRow = 0; iRow < rowsInClipboard.Length; iRow++)
            {
                // Split row into cell values
                string[] valuesInRow = rowsInClipboard[iRow].Split(columnSplitter);

                // Cycle through cell values
                for (int iCol = 0; iCol < valuesInRow.Length; iCol++)
                {

                    // Assign cell value, only if it within columns of the grid
                    if (grid.ColumnCount - 1 >= c + iCol)
                    {
                        DataGridViewCell cell = grid.Rows[r + iRow].Cells[c + iCol];

                        if (!cell.ReadOnly)
                        {
                            cell.Value = valuesInRow[iCol];
                        }
                    }
                }
            }
        }
    }
}

这篇关于DataGridview中的行复制/粘贴功能(Windows应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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