【问题标题】:Dynamically change DataGridView when pasting Excel cells in C#在 C# 中粘贴 Excel 单元格时动态更改 DataGridView
【发布时间】:2018-04-20 06:21:07
【问题描述】:

我正在尝试在 C# 中向我的 Windows 窗体 DataGridView 添加粘贴功能。我已经有了一些可以正常工作的代码,但前提是 DataGridView 有“足够”的列和行。

假设我复制了一个包含五行的 Excel 列,我想将它粘贴到 DataGridView 中的一个列中,其中包含四行,最后一行被截断。我的目标是应该添加这条线,以便粘贴的值也可以添加。

这是我的代码:

string s = Clipboard.GetText();
            string[] lines = s.Split('\n');
            int row = dataGridView1.CurrentCell.RowIndex;
            int col = dataGridView1.CurrentCell.ColumnIndex;
            foreach (string line in lines)
            {
                if (row < dataGridView1.RowCount && line.Length >0)
                {
                    string[] cells = line.Split('\t');
                    for (int i = 0; i < cells.GetLength(0); ++i)
                    {
                        if (col + i < this.dataGridView1.ColumnCount)
                        {
                            dataGridView1[col + i, row].Value = Convert.ChangeType(cells[i], dataGridView1[col + i, row].ValueType);
                        }
                        else
                        {
                            break;
                        }
                    }
                    row++;
                }
                else
                {
                    break;
                }
            }

【问题讨论】:

  • 您在问如何将新的columnrow 添加到datagridview?你需要让你的问题更具体。
  • 我在问,当粘贴的单元格的范围不适合 datagridview 的范围时,如何使用此代码添加新的列或行
  • 您可以将其拆分为子任务:1)检测没有足够的行; 2)添加行; 3) 检测到没有足够的列 4) 添加列。哪一个对你有问题?你需要专注于一个并提出一个具体的问题,否则你的问题太宽泛了
  • 好的,您需要编辑您的问题以使其更清晰。现在,让我们专注于 1:您已经在使用 dataGridView1.RowCount 并且您已经在跟踪当前行号 (int row)。比较这些值应该是微不足道的,不是吗?你能澄清一下这里有什么问题吗?
  • 谢谢,这让我走上了解决问题的正确道路

标签: c# excel winforms datagridview


【解决方案1】:

这段代码现在解决了我的问题:

 string s = Clipboard.GetText();
        string[] lines = s.Split('\n');
        int row = dataGridView1.CurrentCell.RowIndex;
        int col = dataGridView1.CurrentCell.ColumnIndex;
        while (lines.Length != (dataGridView1.RowCount -row))
        {
            dt.Rows.Add();
        }
        foreach (string line in lines)
        {
            if (row < dataGridView1.RowCount && line.Length >0)
            {

                string[] cells = line.Split('\t');
                while(cells.Length != (dataGridView1.ColumnCount - col))
                {
                    dt.Columns.Add();
                }
                for (int i = 0; i < cells.GetLength(0); ++i)
                {
                    if (col + i < this.dataGridView1.ColumnCount)
                    {
                        dataGridView1[col + i, row].Value = Convert.ChangeType(cells[i], dataGridView1[col + i, row].ValueType);
                    }
                    else
                    {
                        break;
                    }
                }
                row++;
            }
            else
            {
                break;
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 2015-02-07
    • 2017-12-30
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多