How to Copy Contents in one DataGridview to another DataGridview

前端 未结 3 1082
有刺的猬
有刺的猬 2021-01-16 07:00

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 Re         


        
3条回答
  •  春和景丽
    2021-01-16 07:09

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

提交回复
热议问题