first time posting. So I'm making this program in visual studio 2013 in VB.NET
Lets say I have 2 forms:
Form1 and Form2 and on Form1 I have DataGridView1 and on form 2 I have DataGridView2
Both data grids have the same columns.
How can I on the click event of a button on Form1 Get all the data from DataGridView1 from the row i'm currently focused/selected on to be transferred into DataGridView2 on Form2.
Image of what i'm trying to do.
If anyone can help me out with this one it would be much appreciated. Thanks
Try this,
'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
First we really have to know you are using databound DataGridView or not.
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
Form2:
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
Form2:
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
Thanks everyone for the reply's but I ended up doing this:
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
Not sure if this is the best way to do it or if its good coding practice but It seem's to work fine and it suit's my purposes. Thanks
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.