Datagridview Select Parent Table Primary Key should display Childs Table Row

donotenter

Member
Joined
Jul 27, 2013
Messages
6
Programming Experience
Beginner
Newbie here!
Is it possible if a parents primary key is selected, multi childs tables primary key should also be highlighted/selected.
If its possible can you create codes for this. I really dont have any idea how to make it. Not yet familiar of the codes could be use.
You could use any way you want. OnCellClick event. using Command Buttons or checkbox.

this is how it suppose to be.
todo1.jpg
 
E.g.
Private Sub parentBindingSource_CurrentChanged(sender As Object, e As EventArgs) Handles parentBindingSource.CurrentChanged
    If parentBindingSource.Current IsNot Nothing Then
        'Get the DataRow selected in the parent grid.
        Dim parentRow = DirectCast(parentBindingSource.Current, DataRowView)

        'Get the ID of the selected row.
        Dim parentId = CInt(parentRow("ID"))

        'Select the row with the same parent ID in the first child grid.
        child1BindingSource.Position = child1BindingSource.Find("ParentID", parentId)

        'Select the row with the same parent ID in the second child grid.
        child2BindingSource.Position = child2BindingSource.Find("ParentID", parentId)
    End If
End Sub
This assumes that you have bound your three DataTables to your three DataGridViews via three BindingSources. You would obviously change the names appropriately.
 
Hi,

Based on the image that you posted, I see why jmcilhinney opted for the post that he made.

If your ultimate goal however is to show multiple related courses and multiple related telephone numbers in your two child tables after a selection is made in the parent table then you should really be looking into Master/Detail, also known as Parent/Child, data binding techniques. In this case have a look at this post, also made by jmcilhinney:-

Master/Detail (Parent/Child) Data-binding (.NET 2.0+ WinForms)-VBForums

Hope that helps.

Cheers,

Ian
 
Thanks you masters!

This is what i did

Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
If FullNameTableBindingSource.Current IsNot Nothing Then

'Get the DataRow selected in the parent grid.
Dim parentRow = DirectCast(FullNameTableBindingSource.Current, DataRowView)

'Get the ID of the selected row.
Dim parentId = CInt(parentRow("StudenID"))

'Select the row with the same parent ID in the first child grid.
CourseBindingSource.Position = CourseBindingSource.Find("StudenID", parentId)

End If
Else
Me.DataGridView1.ClearSelection()
Me.DataGridView2.ClearSelection()
Me.DataGridView3.ClearSelection()
End If

End Sub

Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
If CheckBox2.Checked = True Then
If FullNameTableBindingSource.Current IsNot Nothing Then
'Get the DataRow selected in the parent grid.
Dim parentRow = DirectCast(FullNameTableBindingSource.Current, DataRowView)

'Get the ID of the selected row.
Dim parentId = CInt(parentRow("StudenID"))

'Select the row with the same parent ID in the first child grid.
CourseBindingSource.Position = CourseBindingSource.Find("StudenID", parentId)

'Select the row with the same parent ID in the second child grid.
ContactNumberBindingSource.Position = ContactNumberBindingSource.Find("StudenID", parentId)
End If
Else
Me.DataGridView1.ClearSelection()
Me.DataGridView2.ClearSelection()
Me.DataGridView3.ClearSelection()
End If
End Sub

Result!

tada!.jpg

I use check box because i dont know how to use it in OnCellClickEvent.
Now im reading about the codes ive used so that i can fully understand their function.

Thank You Very Much! Hope I can still ask help from you in the future even being so spoonfeeder.
 
Back
Top