vb.net 如何将一个 DataGridview 中的内容复制到另一个 DataGridview
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25461139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Copy Contents in one DataGridview to another DataGridview
提问by Roman
I want to copy from one datagridview to another datagridview.
我想从一个 datagridview 复制到另一个 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
回答by Bj?rn-Roger Kringsj?
The problem is that you're adding a new row for each and every cell in ReadDataDataGridView
. You need to create only onerow in each row iteration. As of now, you're creating n
rows in each row iteration (where n
is the number of columns).
问题是您要为ReadDataDataGridView
. 您只需要创建一个在每一行迭代行。截至目前,您正在n
每行迭代中创建行(其中n
是列数)。
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#
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());
回答by Pradnya Bolli
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
回答by MhDG7
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