问题描述
Hello Experts,
我有两个datagridviews。双击Datagridview1的单元格,该行应该被复制到Datagridview2。现在,我只能做一行。
例如:
如果Datagridview1有3行。我首先点击了第一行,它被复制到Datagridview2。现在,如果我点击第3行,它应该被附加到Datagridview2。相反,它清除第一个复制的行并替换为第三行。但是我希望1和3在Datagridview2中。
这是我的代码
Hello Experts,
I have Two datagridviews. On double click on cell of Datagridview1 that row should get copied to Datagridview2. For now, I am able to do only one row.
For Example:
If Datagridview1 has 3 rows. I first clicked on 1st row, it gets copied to Datagridview2. Now if I click on 3rd row, it should get appended to Datagridview2. Instead it clears out the 1st copied row and replaces with 3rd row. But I want 1 and 3 to be in Datagridview2.
Here is my code
Private Sub dgvShow_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvShow.CellMouseDoubleClick
Me.Cursor = Cursors.WaitCursor
Dim dgv As DataGridView = sender
Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
checkBoxColumn.HeaderText = "Select"
checkBoxColumn.Width = 40
checkBoxColumn.Name = "checkBoxColumn"
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim hitTestInfo As DataGridView.HitTestInfo = dgv.HitTest(e.X, e.Y)
Dim row As DataGridViewRow = dgv.CurrentRow
Dim dt As New DataTable
For Each col As DataGridViewColumn In dgvShow.Columns
dt.Columns.Add(col.HeaderText)
Next
Dim dRow As DataRow = dt.NewRow()
For Each cell As DataGridViewCell In row.Cells
dRow(cell.ColumnIndex) = cell.Value
Next
dt.Rows.Add(dRow)
DataGridView2.DataSource = dt
DataGridView2.Columns.Insert(0, checkBoxColumn)
End If
Me.Cursor = Cursors.Default
End Sub
请帮帮我。这非常重要。
Please help me. This is really important.
推荐答案
'dgvShow cell Mouse doublleclick
Private Sub dgvShow_CellMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvShow.CellMouseDoubleClick
If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
Dim selectedRow = dgvShow.Rows(e.RowIndex)
Dim index As Integer = DataGridView2.Rows.Add(TryCast(dgvShow.CurrentRow.Clone(), DataGridViewRow))
For Each o As DataGridViewCell In dgvShow.CurrentRow.Cells
DataGridView2.Rows(index).Cells(o.ColumnIndex).Value = o.Value
Next
dgvShow.Rows.RemoveAt(dgvShow.CurrentRow.Index)
End If
End Sub
每行双击它将被添加到Datagridview2。如果已经存在于Datagridview2中,它不会查找重复。
On each row's double click it will get added to Datagridview2. It doesn't look for duplicate if already exists in Datagridview2.
这篇关于在单元格上双击Datagridview1的行应该复制到Datagridview2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!