如何将一个DataGridview中的内容复制到另一个DataGridview [英] How to Copy Contents in one DataGridview to another DataGridview

查看:233
本文介绍了如何将一个DataGridview中的内容复制到另一个DataGridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个datagridview复制到另一个datagridview。



我尝试了下面的代码,但是我仍然拥有第一列中的所有数据:

 对于c = 0 To ReadDataDataGridView.Rows.Count  -  1 
对于t = 0 To ReadDataDataGridView.Columns.Count - 1
DataGridView1.Rows.Add(ReadDataDataGridView.Rows(c).Cells(t).Value)
下一个
下一个


解决方案

问题是您为 ReadDataDataGridView 。您需要在每一行迭代中仅创建一个行。截至目前,您正在每行迭代中创建 n 行(其中 n 是列数)。



这里有一种方法:



VB.NET

 '引用源和目标网格。 

Dim sourceGrid As DataGridView = Me.DataGridView1
Dim targetGrid As DataGridView = Me.DataGridView2

'复制所有行和单元格。

Dim targetRows =新列表(DataGridViewRow)

对于每个sourceRow作为DataGridViewRow在sourceGrid.Rows

If(Not sourceRow.IsNewRow)Then

Dim targetRow = CType(sourceRow.Clone(),DataGridViewRow)

'克隆方法不复制单元格值,因此我们必须手动执行。
'请参阅:https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.clone(v=vs.110).aspx

对于每个单元格作为DataGridViewCell在sourceRow.Cells
targetRow.Cells(cell.ColumnIndex).Value = cell.Value
下一个

targetRows.Add(targetRow)

结束如果

下一个

'清除目标列,然后克隆所有源列。

targetGrid.Columns.Clear()

对于每列作为DataGridViewColumn在sourceGrid.Columns
targetGrid.Columns.Add(CType(column.Clone(), DataGridViewColumn))
下一个

'当向集合中添加多个项目时,建议使用AddRange方法(如果可用)
'。

targetGrid.Rows.AddRange(targetRows.ToArray())

C#

  //引用源和目标网格。 

DataGridView sourceGrid = this.dataGridView1;
DataGridView targetGrid = this.dataGridView2;

//复制所有行和单元格。

var targetRows = new List< DataGridViewRow>();

foreach(sourceGrid.Rows中的DataGridViewRow sourceRow)
{

if(!sourceRow.IsNewRow)
{

var targetRow =(DataGridViewRow)sourceRow.Clone();

//克隆方法不复制单元格值,因此我们必须手动执行。
//参见:https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.clone(v=vs.110).aspx

foreach(sourceRow.Cells中的DataGridViewCell单元格)
{
targetRow.Cells [cell.ColumnIndex] .Value = cell.Value;
}

targetRows.Add(targetRow);

}

}

//清除目标列,然后克隆所有源列。

targetGrid.Columns.Clear();

foreach(sourceGrid.Columns中的DataGridViewColumn列)
{
targetGrid.Columns.Add((DataGridViewColumn)column.Clone());
}

//在集合中添加多个项目时,建议使用AddRange方法(如果可用)
//。

targetGrid.Rows.AddRange(targetRows.ToArray());


I want to copy from one datagridview to another datagridview.

I tried the code below but I still have all data in the first columns :

For c = 0 To ReadDataDataGridView.Rows.Count - 1
    For t = 0 To ReadDataDataGridView.Columns.Count - 1
        DataGridView1.Rows.Add(ReadDataDataGridView.Rows(c).Cells(t).Value)
    Next
Next

解决方案

The problem is that you're adding a new row for each and every cell in ReadDataDataGridView. You need to create only one row in each row iteration. As of now, you're creating n rows in each row iteration (where n is the number of columns).

Here's one way to do it:

VB.NET

'References to source and target grid.

Dim sourceGrid As DataGridView = Me.DataGridView1
Dim targetGrid As DataGridView = Me.DataGridView2

'Copy all rows and cells.

Dim targetRows = New List(Of DataGridViewRow)

For Each sourceRow As DataGridViewRow In sourceGrid.Rows

    If (Not sourceRow.IsNewRow) Then

        Dim targetRow = CType(sourceRow.Clone(), DataGridViewRow)

        'The Clone method do not copy the cell values so we must do this manually.
        'See: https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.clone(v=vs.110).aspx

        For Each cell As DataGridViewCell In sourceRow.Cells
            targetRow.Cells(cell.ColumnIndex).Value = cell.Value
        Next

        targetRows.Add(targetRow)

    End If

Next

'Clear target columns and then clone all source columns.

targetGrid.Columns.Clear()

For Each column As DataGridViewColumn In sourceGrid.Columns
    targetGrid.Columns.Add(CType(column.Clone(), DataGridViewColumn))
Next

'It's recommended to use the AddRange method (if available)
'when adding multiple items to a collection.

targetGrid.Rows.AddRange(targetRows.ToArray())

C#

//References to source and target grid.

DataGridView sourceGrid = this.dataGridView1;
DataGridView targetGrid = this.dataGridView2;

//Copy all rows and cells.

var targetRows = new List<DataGridViewRow>();

foreach (DataGridViewRow sourceRow in sourceGrid.Rows)
{

    if (!sourceRow.IsNewRow)
    {

        var targetRow = (DataGridViewRow)sourceRow.Clone();

        //The Clone method do not copy the cell values so we must do this manually.
        //See: https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.clone(v=vs.110).aspx

        foreach (DataGridViewCell cell in sourceRow.Cells)
        {
            targetRow.Cells[cell.ColumnIndex].Value = cell.Value;
        }

        targetRows.Add(targetRow);

    }

}

//Clear target columns and then clone all source columns.

targetGrid.Columns.Clear();

foreach (DataGridViewColumn column in sourceGrid.Columns)
{
    targetGrid.Columns.Add((DataGridViewColumn)column.Clone());
}

//It's recommended to use the AddRange method (if available)
//when adding multiple items to a collection.

targetGrid.Rows.AddRange(targetRows.ToArray());

这篇关于如何将一个DataGridview中的内容复制到另一个DataGridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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