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
As much as I remember you just need to put this in your code
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
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());
I Hope this work for your project.
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