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

查看:27
本文介绍了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.

我在谷歌上搜索如何粘贴到来自剪贴板的 C# 中的 DataGridView,一个信息,从 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

下面是稍微修改的版本.除了小的重构之外,我禁止粘贴到只读单元格中.

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 = { '
', '
' };
            char[] columnSplitter = { '	' };

            // 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天全站免登陆