使用按钮将内容从一个 datagridview 传递到另一个 datagridview
Pass content from a datagridview to another datagridview using a button
美好的一天。
我试图通过以下方式将 datagridview 的所有内容从子窗体传递到父窗体的另一个 datagridview。
我已经设法通过 gridview 按钮传递数据,但这次它只传递给我 1 条记录,但我试图传递该 datadriview 的所有记录
这是我实现的代码。
子窗体
Private Function BuscarProductos() As DataTable
Dim dt As New DataTable
Try
dt.Columns.Add("IDPRODUCTO")
dt.Columns.Add("DESCRIPCION")
dt.Rows.Add(If(IsDBNull(dgvdetalleproduc.CurrentRow.Cells(0).Value), "", dgvdetalleproduc.CurrentRow.Cells(0).Value),
If(IsDBNull(dgvdetalleproduc.CurrentRow.Cells(0).Value), "", dgvdetalleproduc.CurrentRow.Cells(1).Value))
Catch ex1 As SqlClient.SqlException : MessageBox.Show(ex1.Message, "AUTONORT S.A.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Catch ex As Exception : MessageBox.Show(ex.Message, "AUTONORT S.A.", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Return dt
End Function
添加按钮
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim _dataTable As DataTable = BuscarProductos()
Dim estadoOperacion As Boolean = Me.Openerproducs.BusquedadProduct(_dataTable)
'e.Cancel = Not estadoOperacion
Me.Close()
End Sub
并在父表单中使用此方法接收它
Public Function BusquedadPRODUC(ByVal dataTableParam As DataTable) As Boolean Implements IFormProduc.BusquedadProduct 'BUSCAR POr afiliado
Try
dgvdetalle.Rows.Add(dataTableParam.Rows(0).Item(0), dataTableParam.Rows(0).Item(1))
'Me.dgvdetalle.Columns.Add(dataTableParam.Rows(0).Item(0))
' txtnomproducto.Text = dataTableParam.Rows(0).Item(1)
'dgvMantenimiento.Rows.Add(dataTableParam.Rows(0).Item(0), dataTableParam.Rows(0).Item(1))
'txtCliente.Text =
'dgvRequerimiento.Rows.Add(If(dgvRequerimiento.Rows(dgvRequerimiento.Rows.Count - 1).Cells(0).Value + 1 <= 9, "00" + (dgvRequerimiento.Rows(dgvRequerimiento.Rows.Count - 1).Cells(0).Value + 1).ToString, "0" + (dgvRequerimiento.Rows(dgvRequerimiento.Rows.Count - 1).Cells(0).Value + 1).ToString), rows.Item(2), rows.Item(3), rows.Item(4), rows.Item(6), rows.Item(5))
'Dim formAdd As New frmLineaCredito
'formAdd.MdiParent = Me.MdiParent
'formAdd.Opener = CType(Me, IFormVH)
'formAdd.Text = "Linea de Credito : " + VGlobales.Empresa
'formAdd.frmLineaCredito(lblCodigoClie.Text.Trim)
'formAdd.Show()
Catch ex1 As SqlClient.SqlException : MessageBox.Show(ex1.Message, "hola", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Catch ex As Exception : MessageBox.Show(ex.Message, "hola", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Return True
End Function
这是我在上面的评论中描述的一个简单示例。 Form1
创建并显示两个 MDI children。其中一个 child 表单在 parent 表单处理的 Button
的 Click
上引发了一个事件。 parent 然后从 child 中提取数据,将其推送到另一个 child,然后第二个 child 显示该数据。
Parent:
Public Class Form1
'Source child form
Private WithEvents f2 As Form2
'Destination child form
Private f3 As Form3
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
f2 = New Form2
f2.MdiParent = Me
f2.Show()
f3 = New Form3
f3.MdiParent = Me
f3.Show()
End Sub
Private Sub f2_DataAvailable(sender As Object, e As EventArgs) Handles f2.DataAvailable
'Pull data from source
Dim table = f2.GetData()
'Push data to destination
f3.SetData(table)
End Sub
End Class
来源child:
Public Class Form2
Private table As DataTable
Public Event DataAvailable As EventHandler
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
table = New DataTable()
With table.Columns
.Add("Number", GetType(Integer))
.Add("Text", GetType(String))
End With
BindingSource1.DataSource = table
DataGridView1.DataSource = BindingSource1
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
OnDataAvailable(EventArgs.Empty)
End Sub
Protected Overridable Sub OnDataAvailable(e As EventArgs)
RaiseEvent DataAvailable(Me, e)
End Sub
Public Function GetData() As DataTable
Return table.Copy()
End Function
End Class
目的地child:
Public Class Form3
Public Sub SetData(table As DataTable)
BindingSource1.DataSource = table
DataGridView1.DataSource = BindingSource1
End Sub
End Class
您可以非常轻松地自行测试该代码。只需创建一个具有三种形式的项目,将第一个设为 MDI parent,将一个 DataGridView
、一个 BindingSource
和一个 Button
添加到第二个和一个 DataGridView
第三个 BindingSource
就可以了。
美好的一天。
我试图通过以下方式将 datagridview 的所有内容从子窗体传递到父窗体的另一个 datagridview。
我已经设法通过 gridview 按钮传递数据,但这次它只传递给我 1 条记录,但我试图传递该 datadriview 的所有记录
这是我实现的代码。
子窗体
Private Function BuscarProductos() As DataTable
Dim dt As New DataTable
Try
dt.Columns.Add("IDPRODUCTO")
dt.Columns.Add("DESCRIPCION")
dt.Rows.Add(If(IsDBNull(dgvdetalleproduc.CurrentRow.Cells(0).Value), "", dgvdetalleproduc.CurrentRow.Cells(0).Value),
If(IsDBNull(dgvdetalleproduc.CurrentRow.Cells(0).Value), "", dgvdetalleproduc.CurrentRow.Cells(1).Value))
Catch ex1 As SqlClient.SqlException : MessageBox.Show(ex1.Message, "AUTONORT S.A.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Catch ex As Exception : MessageBox.Show(ex.Message, "AUTONORT S.A.", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Return dt
End Function
添加按钮
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim _dataTable As DataTable = BuscarProductos()
Dim estadoOperacion As Boolean = Me.Openerproducs.BusquedadProduct(_dataTable)
'e.Cancel = Not estadoOperacion
Me.Close()
End Sub
并在父表单中使用此方法接收它
Public Function BusquedadPRODUC(ByVal dataTableParam As DataTable) As Boolean Implements IFormProduc.BusquedadProduct 'BUSCAR POr afiliado
Try
dgvdetalle.Rows.Add(dataTableParam.Rows(0).Item(0), dataTableParam.Rows(0).Item(1))
'Me.dgvdetalle.Columns.Add(dataTableParam.Rows(0).Item(0))
' txtnomproducto.Text = dataTableParam.Rows(0).Item(1)
'dgvMantenimiento.Rows.Add(dataTableParam.Rows(0).Item(0), dataTableParam.Rows(0).Item(1))
'txtCliente.Text =
'dgvRequerimiento.Rows.Add(If(dgvRequerimiento.Rows(dgvRequerimiento.Rows.Count - 1).Cells(0).Value + 1 <= 9, "00" + (dgvRequerimiento.Rows(dgvRequerimiento.Rows.Count - 1).Cells(0).Value + 1).ToString, "0" + (dgvRequerimiento.Rows(dgvRequerimiento.Rows.Count - 1).Cells(0).Value + 1).ToString), rows.Item(2), rows.Item(3), rows.Item(4), rows.Item(6), rows.Item(5))
'Dim formAdd As New frmLineaCredito
'formAdd.MdiParent = Me.MdiParent
'formAdd.Opener = CType(Me, IFormVH)
'formAdd.Text = "Linea de Credito : " + VGlobales.Empresa
'formAdd.frmLineaCredito(lblCodigoClie.Text.Trim)
'formAdd.Show()
Catch ex1 As SqlClient.SqlException : MessageBox.Show(ex1.Message, "hola", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Catch ex As Exception : MessageBox.Show(ex.Message, "hola", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Return True
End Function
这是我在上面的评论中描述的一个简单示例。 Form1
创建并显示两个 MDI children。其中一个 child 表单在 parent 表单处理的 Button
的 Click
上引发了一个事件。 parent 然后从 child 中提取数据,将其推送到另一个 child,然后第二个 child 显示该数据。
Parent:
Public Class Form1
'Source child form
Private WithEvents f2 As Form2
'Destination child form
Private f3 As Form3
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
f2 = New Form2
f2.MdiParent = Me
f2.Show()
f3 = New Form3
f3.MdiParent = Me
f3.Show()
End Sub
Private Sub f2_DataAvailable(sender As Object, e As EventArgs) Handles f2.DataAvailable
'Pull data from source
Dim table = f2.GetData()
'Push data to destination
f3.SetData(table)
End Sub
End Class
来源child:
Public Class Form2
Private table As DataTable
Public Event DataAvailable As EventHandler
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
table = New DataTable()
With table.Columns
.Add("Number", GetType(Integer))
.Add("Text", GetType(String))
End With
BindingSource1.DataSource = table
DataGridView1.DataSource = BindingSource1
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
OnDataAvailable(EventArgs.Empty)
End Sub
Protected Overridable Sub OnDataAvailable(e As EventArgs)
RaiseEvent DataAvailable(Me, e)
End Sub
Public Function GetData() As DataTable
Return table.Copy()
End Function
End Class
目的地child:
Public Class Form3
Public Sub SetData(table As DataTable)
BindingSource1.DataSource = table
DataGridView1.DataSource = BindingSource1
End Sub
End Class
您可以非常轻松地自行测试该代码。只需创建一个具有三种形式的项目,将第一个设为 MDI parent,将一个 DataGridView
、一个 BindingSource
和一个 Button
添加到第二个和一个 DataGridView
第三个 BindingSource
就可以了。