Hi guys,
I am new to vb.net i want to know how to pass a datagridview row values onto another form once the row is been double clicked.
Hi guys,
I am new to vb.net i want to know how to pass a datagridview row values onto another form once the row is been double clicked.
A small but important improvement is to pass data, not UI objects. Instead of sending a DataGridViewSelectedRowCollection to the second form, handle the grid’s CellDoubleClick, grab the underlying data for the clicked row, and pass a lightweight model (or DataRow) via the form’s constructor. This avoids coupling forms to a UI control that may be disposed, and it works whether one or multiple rows are selected.
Example pattern (data-bound to a DataTable):
Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellDoubleClick
If e.RowIndex < 0 Then Return ' ignore header
Dim row = DataGridView1.Rows(e.RowIndex)
Dim drv = TryCast(row.DataBoundItem, DataRowView)
If drv Is Nothing Then Return
Using f As New DetailsForm(drv.Row) ' pass the DataRow
f.ShowDialog(Me)
End Using
End Sub And in DetailsForm:
Public Sub New(dr As DataRow)
InitializeComponent()
' read values, e.g. TextBoxName.Text = CStr(dr("Name"))
End Sub If your grid is unbound, create a DTO class and map from row.Cells("ColName").Value instead. Set SelectionMode = FullRowSelect and consider MultiSelect = False to simplify. For edits that should persist, bind to a BindingSource and call EndEdit/Update on your TableAdapter. See CellDoubleClick, DataBoundItem, and DataRowView for details.
Jump to Post— djjeavons 113Hi
One way to do this would be to use the SelectedRows property of the DataGridView and when it is double clicked you could pass this to your next form. For example, if I had two Forms (Form1 and Form2) and Form1 contained a DataGridView I could set it up …
Hi
One way to do this would be to use the SelectedRows property of the DataGridView and when it is double clicked you could pass this to your next form. For example, if I had two Forms (Form1 and Form2) and Form1 contained a DataGridView I could set it up so that when the DataGridView is double clicked and one or more rows was selected I could pass these to Form2 via a property.
So, on Form2 you could have the following property:
Public Property SelectedRows As DataGridViewSelectedRowCollection
This would appear just below the Public Class Form2 line.
Then in Form1, for the DoubleClick event of the DataGridView you could do the following:
Private Sub DataGridView1_DoubleClick(sender As Object, e As EventArgs) Handles DataGridView1.DoubleClick
If DataGridView1.SelectedRows.Count > 0 Then
Dim newForm As New Form2
newForm.SelectedRows = DataGridView1.SelectedRows
newForm.Show()
End If
End Sub
Then to read from the SelectedRows property on Form2 you could use code similar too:
MessageBox.Show(SelectedRows(0).Cells(1).Value)
Which will return the value of the first row, second column.
HTH
thank you soo much djjeavons. it worked just as i wanted it to..
I have two forms. i already have some values has been displayed in datagrid view in one form including name. In another form, i additionally add some more values including name field. I need to pass the second form values to the datagrid which was in first form by comparing name fields.
I have tried the following code example above to my project without any success can someone please help.
Here is a copy of my code
On the schedule_frm i declared: Public Property SelectRows As DataGridViewSelectedRowCollection
under the Public Class Schedule_Frm form class
Public Class Main_Frm
Private Sub TBL_TEAMSBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles TBL_TEAMSBindingNavigatorSaveItem.Click
Me.Validate()
Me.TBL_TEAMSBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.FootBallDataSet)
End Sub
Private Sub Main_Frm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'FootBallDataSet.TBL_TEAMS' table. You can move, or remove it, as needed.
Me.TBL_TEAMSTableAdapter.Fill(Me.FootBallDataSet.TBL_TEAMS)
End Sub
Private Sub TBL_TEAMSDataGridView_Click(sender As Object, e As EventArgs) Handles TBL_TEAMSDataGridView.Click
If TBL_TEAMSDataGridView.SelectedRows.Count Then
Dim Schedule_Form As New Schedule_Frm
Schedule_Form.SelectRows = TBL_TEAMSDataGridView.SelectedRows
Schedule_Form.ShowDialog()
End If
End Sub End Class
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.