将 DataGridView 的行复制到另一个 DataGridView

发布于 2024-11-15 09:29:23 字数 514 浏览 1 评论 0原文

所以基本上我有 2 个 DataGridView,我需要将行从一个复制到另一个。

到目前为止我已经尝试过:

DataGridViewRowCollection tmpRowCollection = DataGridView1.Rows;

DataGridViewRow[] tmpRowArray = new DataGridViewRow[tmpRowCollection.Count];
tmpRowCollection.CopyTo(tmpRowArray, 0);            

DataGridView2.Rows.AddRange((DataGridViewRow[]) tmpRowArray));

但它一直说

"Row provided already belongs to a DataGridView control."

那么复制行内容的最佳方法是什么(两个 DataGridView 都有相同的列)?

So basically I've got 2 DataGridView and I need to copy the rows from one to the other.

So far I've tried:

DataGridViewRowCollection tmpRowCollection = DataGridView1.Rows;

DataGridViewRow[] tmpRowArray = new DataGridViewRow[tmpRowCollection.Count];
tmpRowCollection.CopyTo(tmpRowArray, 0);            

DataGridView2.Rows.AddRange((DataGridViewRow[]) tmpRowArray));

But it keeps saying that

"Row provided already belongs to a DataGridView control."

So what's the best way to copy the content of the rows (both DataGridView have the same columns) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笛声青案梦长安 2024-11-22 09:29:23

您可以通过以下链接使用该函数

private DataGridView CopyDataGridView(DataGridView dgv_org)
{
    DataGridView dgv_copy = new DataGridView();
    try
    {
        if (dgv_copy.Columns.Count == 0)
        {
            foreach (DataGridViewColumn dgvc in dgv_org.Columns)
            {
                dgv_copy.Columns.Add(dgvc.Clone() as DataGridViewColumn);
            }
        }

        DataGridViewRow row = new DataGridViewRow();

        for (int i = 0; i < dgv_org.Rows.Count; i++)
        {
            row = (DataGridViewRow)dgv_org.Rows[i].Clone();
            int intColIndex = 0;
            foreach (DataGridViewCell cell in dgv_org.Rows[i].Cells)
            {
                row.Cells[intColIndex].Value = cell.Value;
                intColIndex++;
            }
            dgv_copy.Rows.Add(row);
        }
        dgv_copy.AllowUserToAddRows = false;
        dgv_copy.Refresh();

    }
    catch (Exception ex)
    {
        cf.ShowExceptionErrorMsg("Copy DataGridViw", ex);
    }
    return dgv_copy;
}

http://canlu.blogspot .com/2009/06/copying-datagridviewrow-to-another.html

You use the function at the following link

private DataGridView CopyDataGridView(DataGridView dgv_org)
{
    DataGridView dgv_copy = new DataGridView();
    try
    {
        if (dgv_copy.Columns.Count == 0)
        {
            foreach (DataGridViewColumn dgvc in dgv_org.Columns)
            {
                dgv_copy.Columns.Add(dgvc.Clone() as DataGridViewColumn);
            }
        }

        DataGridViewRow row = new DataGridViewRow();

        for (int i = 0; i < dgv_org.Rows.Count; i++)
        {
            row = (DataGridViewRow)dgv_org.Rows[i].Clone();
            int intColIndex = 0;
            foreach (DataGridViewCell cell in dgv_org.Rows[i].Cells)
            {
                row.Cells[intColIndex].Value = cell.Value;
                intColIndex++;
            }
            dgv_copy.Rows.Add(row);
        }
        dgv_copy.AllowUserToAddRows = false;
        dgv_copy.Refresh();

    }
    catch (Exception ex)
    {
        cf.ShowExceptionErrorMsg("Copy DataGridViw", ex);
    }
    return dgv_copy;
}

http://canlu.blogspot.com/2009/06/copying-datagridviewrow-to-another.html

玉环 2024-11-22 09:29:23

您需要首先从原始视图克隆行,然后添加到新视图。
http://msdn.microsoft.com/en -us/library/system.windows.forms.datagridviewrow.clone.aspx

you need to first clone the row from the original then add to new view.
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.clone.aspx

与之呼应 2024-11-22 09:29:23

我建议为此使用支持 DTO。不要直接处理行,而是创建一个包含 GridView 的所有列的 DTO,然后使用它们的列表作为数据源。然后,添加/删除行所需要做的就是在列表中添加/删除 DTO。

I would recommend using a backing DTO for this. Instead of dealing with the rows directly, create a DTO that contains all the columns of your GridViews, then use a List of them as your DataSource. Then, all you have to do to add/remove rows is to add/remove DTOs in the list.

把回忆走一遍 2024-11-22 09:29:23

就这样写:

copyDGV.DataSource = mainDGV.DataSource;        

just write this:

copyDGV.DataSource = mainDGV.DataSource;        
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文