如何将一个DataGridview中的内容复制到另一个DataGridview

时间:2014-08-23 10:52:22

标签: vb.net datagridview

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

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

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

3 个答案:

答案 0 :(得分:3)

问题是您要为ReadDataDataGridView中的每个单元格添加新行。您需要在每行迭代中仅创建一个行。截至目前,您在每行迭代中创建n行(其中n是列数)。

以下是一种方法:

<强> 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());

答案 1 :(得分:0)

我记得你只需要把它放在你的代码中

For Each row As DataGridViewRow In classDataGridView.SelectedRows
        Dim text As String
        For Each cell As DataGridViewCell In classDataGridView.SelectedCells
            text = cell.Value.ToString
            For Each scheduleCell As DataGridViewCell In scheduleDataGridView.SelectedCells
                scheduleCell.Value.ToString.Equals(text)
            Next scheduleCell
        Next cell
    Next

答案 2 :(得分:0)

我希望这对你的项目有用。

DataGridView1.Columns.Clear()
            For Each Col As DataGridViewColumn In DataGridView2.Columns
                DataGridView1.Columns.Add(DirectCast(Col.Clone, DataGridViewColumn))
            Next
            For rowIndex As Integer = 0 To (DataGridView2.Rows.Count - 1)
                DataGridView1.Rows.Add(DataGridView2.Rows(rowIndex).Cells.Cast(Of DataGridViewCell).Select(Function(c) c.Value).ToArray)
            Next