第一次发帖。 所以我在VB.NET的visual studio 2013中制作了这个程序
假设我有两种形式:
Form1和Form2,在Form1上我有DataGridView1,在表单2上我有DataGridView2
两个数据网格都有相同的列。
如何在Form1上按钮的click事件中获取DataGridView1中当前聚焦/选中的行中的所有数据,以便将其传输到Form2上的DataGridView2中。
我想要做的事情的图像。
如果有人能帮我解决这个问题,我将不胜感激。感谢
答案 0 :(得分:0)
试试这个,
'Form1-- following code will be placed in button click
Dim cells(grd.Columns.Count) As New Object
For c As int = 0 To grd.Columns.Count
cells(c) = grd.CurrentRow.Cells(c).Value
Next
Dim frm As Form1 = Application.OpenForms("frmName")
frm.sendRow(cells)
'Form2 -- Create a method that will add all cell values from form 1
Public Sub sendRow(ByVal cells() As Object)
grd.Rows.Add(cells)
End Sub
答案 1 :(得分:0)
首先,我们必须知道您正在使用数据绑定DataGridView。
<强> Form1中:强>
dim dt as new DataTable()
// Fetch data from DB and assign it to DataSource.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
// Run query to fetch data
// dt = GetTableByRunningQuery
dataGridView1.DataSource = dt
End Sub
Private Sub btnTransfer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm2 as new Form2()
frm2.dataSource = dt
frm2.Show()
End Sub
<强>窗体2:强>
Public dataSource as Object
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
dataGridView1.DataSource = dataSource
End Sub
<强> Form1中:强>
Private Sub btnTransfer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm2 as new Form2()
frm2.SetRows(dataGridView1.Rows)
frm2.Show()
End Sub
<强>窗体2:强>
Public Sub SetRows(ByVal rows as DataGridViewRowCollection)
For each row as DataGridViewRow in rows
Dim newRow as DataGridViewRow = Ctype(row.Clone(), DataGridViewRow)
dataGridView1.Rows.Add(newRow)
Next
End Sub
答案 2 :(得分:0)
感谢大家的回复,但我最终这样做了:
Private Sub searchPartsDataGridView_CellContentClicked(sender As Object, e As DataGridViewCellEventArgs) Handles searchPartsDataGridView.CellContentDoubleClick
Dim colName As String = searchPartsDataGridView.Columns(e.ColumnIndex).Name()
Dim rowIndex = searchPartsDataGridView.CurrentRow.Index
If colName = "ITEMNO" Then
formPartsRequest.partsRequestItemsDataGridView.Rows.Add(searchPartsDataGridView.Rows(rowIndex).Cells("ITEMNO").Value, searchPartsDataGridView.Rows(rowIndex).Cells("DESC").Value)
End If
End Sub
不确定这是否是最好的方法,或者它的良好编码习惯,但似乎工作正常,这符合我的目的。感谢