Page 2 of 2 FirstFirst 12
Results 41 to 47 of 47

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

  1. #41
    Addicted Member coolwater's Avatar
    Join Date
    Dec 2004
    Location
    philippines
    Posts
    215

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

    ok thanks.

  2. #42
    Junior Member
    Join Date
    Dec 2014
    Posts
    23

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

    I have been spending hours looking for this. Thank you very much.

  3. #43
    Junior Member
    Join Date
    Dec 2014
    Posts
    23

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

    I have been working on a modification of this code for several hours now and had a few questions. I have all of the parent and child items that I want for my program Parent= (Printer, Camera, Blackberry, MiFI) in combobox1 and the models of those devices in the child combobox2. This is exactly what I want however I also want to add buttons that after you select the model in combobox2, hitting the button will execute a manual for that model.

    Code:
    Public Class Form1
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    
        End Sub
    
        Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
    
        End Sub
    
        Private Sub BindingSource1_CurrentChanged(sender As Object, e As EventArgs) Handles BindingSource1.CurrentChanged
    
        End Sub
        Private Sub Form1_Load(ByVal sender As Object, _
                           ByVal e As EventArgs) Handles MyBase.Load
            'Get the data.  The DataSet must contain a Parent table,
            'a Child table and a ParentChild relation between them
            Dim data As DataSet = Me.GetDataSet()
    
            'Bind the parent source to the parent table.
            Me.BindingSource1.DataSource = data
            Me.BindingSource1.DataMember = "Parent"
    
            'Bind the child source to the relationship.
            Me.BindingSource2.DataSource = Me.BindingSource1
            Me.BindingSource2.DataMember = "ParentChild"
    
            'Bind the parent control to the parent source.
            Me.ComboBox1.DisplayMember = "Name"
            Me.ComboBox1.ValueMember = "ID"
            Me.ComboBox1.DataSource = Me.BindingSource1
    
            'Bind the child control to the child source.
            Me.ComboBox2.DisplayMember = "Name"
            Me.ComboBox2.ValueMember = "ID"
            Me.ComboBox2.DataSource = Me.BindingSource2
        End Sub
    
        Private Function GetDataSet() As DataSet
            Dim data As New DataSet
            Dim parent As DataTable = Me.GetParentTable()
            Dim child As DataTable = Me.GetChildTable()
    
            data.Tables.Add(parent)
            data.Tables.Add(child)
    
            'Add a relationship between the ID of the parent
            'table and the ParentID of the child table.
            data.Relations.Add("ParentChild", _
                               parent.Columns("ID"), _
                               child.Columns("ParentID"))
    
            Return data
        End Function
    
        Private Function GetParentTable() As DataTable
            Dim table As New DataTable("Parent")
    
            With table.Columns
                .Add("ID", GetType(Integer))
                .Add("Name", GetType(String))
            End With
    
            With table.Rows
                .Add(1, "Desk Printer")
                .Add(2, "LAN Printer")
                .Add(3, "Blackberry")
                .Add(4, "MiFi")
                .Add(5, "Cameras")
            End With
    
            Return table
        End Function
    
        Private Function GetChildTable() As DataTable
            Dim table As New DataTable("Child")
    
            With table.Columns
                .Add("ID", GetType(Integer))
                .Add("ParentID", GetType(Integer))
                .Add("Name", GetType(String))
            End With
    
            With table.Rows
                .Add(1, 1, "Office Jet Pro 8630")   'Desk Printers Start
                .Add(2, 1, "Office Jet Pro 8600")
                .Add(3, 1, "Office Jet Mobile 150")
                .Add(4, 1, "Office Jet Mobile 100") 'Desk Printers End
                .Add(4, 2, "Oki C530dn")            'LAN Printers Start
                .Add(5, 2, "HP Laserjet P3015x")    'LAN Printers End
                .Add(6, 3, "Emulator 1")                'Blackberry Start
                .Add(7, 3, "Emulator 2")
                .Add(8, 3, "Emulator 3")                'Blackberry End 
                .Add(9, 4, "Verizon Jetpack")                'MiFi Start
                .Add(10, 4, "ATT Hotspot")
                .Add(11, 4, "Child 11")             'MiFi End
                .Add(12, 5, "Powershot A1400")             'Camera Start
                .Add(13, 5, "Powershot A1300")             'Camera End
            End With
    
            Return table
        End Function
    
        Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Front View
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 'Back View
    
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 'Open Simulator
            If Me.ComboBox2.ValueMember = "Emulator 1" Then System.Diagnostics.Process.Start("E:\Emulator Tool Project\Blackberry Sim\9000att\fledge.exe")
        End Sub
    
        Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click 'Open Manual
    
        End Sub
    End Class
    This is what I have so far I tried adding what I thought would be the proper code for button 3 but when clicking on it nothing happens. Do I need to add another binding source from the child to button?

  4. #44
    Junior Member
    Join Date
    Dec 2014
    Posts
    23

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

    Actually I just figured it out and I was making it harder then it even had to be.

    Code:
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 'Open Simulator
            If ComboBox2.Text = "Office Jet Pro 8600" Then System.Diagnostics.Process.Start("E:\Emulator Tool Project\Printer Sim\8600\8600 display.png")
            If ComboBox2.Text = "Emulator 1" Then System.Diagnostics.Process.Start("E:\Emulator Tool Project\Blackberry Sim\9000att\fledge.exe")
            If ComboBox2.Text = "Emulator 2" Then System.Diagnostics.Process.Start("E:\Emulator Tool Project\Blackberry Sim\9780tmobile\fledge.exe")
        End Sub

  5. #45

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

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

    Quote Originally Posted by Mattw11486 View Post
    I have been working on a modification of this code for several hours now and had a few questions. I have all of the parent and child items that I want for my program Parent= (Printer, Camera, Blackberry, MiFI) in combobox1 and the models of those devices in the child combobox2. This is exactly what I want however I also want to add buttons that after you select the model in combobox2, hitting the button will execute a manual for that model.

    Code:
    Public Class Form1
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    
        End Sub
    
        Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
    
        End Sub
    
        Private Sub BindingSource1_CurrentChanged(sender As Object, e As EventArgs) Handles BindingSource1.CurrentChanged
    
        End Sub
        Private Sub Form1_Load(ByVal sender As Object, _
                           ByVal e As EventArgs) Handles MyBase.Load
            'Get the data.  The DataSet must contain a Parent table,
            'a Child table and a ParentChild relation between them
            Dim data As DataSet = Me.GetDataSet()
    
            'Bind the parent source to the parent table.
            Me.BindingSource1.DataSource = data
            Me.BindingSource1.DataMember = "Parent"
    
            'Bind the child source to the relationship.
            Me.BindingSource2.DataSource = Me.BindingSource1
            Me.BindingSource2.DataMember = "ParentChild"
    
            'Bind the parent control to the parent source.
            Me.ComboBox1.DisplayMember = "Name"
            Me.ComboBox1.ValueMember = "ID"
            Me.ComboBox1.DataSource = Me.BindingSource1
    
            'Bind the child control to the child source.
            Me.ComboBox2.DisplayMember = "Name"
            Me.ComboBox2.ValueMember = "ID"
            Me.ComboBox2.DataSource = Me.BindingSource2
        End Sub
    
        Private Function GetDataSet() As DataSet
            Dim data As New DataSet
            Dim parent As DataTable = Me.GetParentTable()
            Dim child As DataTable = Me.GetChildTable()
    
            data.Tables.Add(parent)
            data.Tables.Add(child)
    
            'Add a relationship between the ID of the parent
            'table and the ParentID of the child table.
            data.Relations.Add("ParentChild", _
                               parent.Columns("ID"), _
                               child.Columns("ParentID"))
    
            Return data
        End Function
    
        Private Function GetParentTable() As DataTable
            Dim table As New DataTable("Parent")
    
            With table.Columns
                .Add("ID", GetType(Integer))
                .Add("Name", GetType(String))
            End With
    
            With table.Rows
                .Add(1, "Desk Printer")
                .Add(2, "LAN Printer")
                .Add(3, "Blackberry")
                .Add(4, "MiFi")
                .Add(5, "Cameras")
            End With
    
            Return table
        End Function
    
        Private Function GetChildTable() As DataTable
            Dim table As New DataTable("Child")
    
            With table.Columns
                .Add("ID", GetType(Integer))
                .Add("ParentID", GetType(Integer))
                .Add("Name", GetType(String))
            End With
    
            With table.Rows
                .Add(1, 1, "Office Jet Pro 8630")   'Desk Printers Start
                .Add(2, 1, "Office Jet Pro 8600")
                .Add(3, 1, "Office Jet Mobile 150")
                .Add(4, 1, "Office Jet Mobile 100") 'Desk Printers End
                .Add(4, 2, "Oki C530dn")            'LAN Printers Start
                .Add(5, 2, "HP Laserjet P3015x")    'LAN Printers End
                .Add(6, 3, "Emulator 1")                'Blackberry Start
                .Add(7, 3, "Emulator 2")
                .Add(8, 3, "Emulator 3")                'Blackberry End 
                .Add(9, 4, "Verizon Jetpack")                'MiFi Start
                .Add(10, 4, "ATT Hotspot")
                .Add(11, 4, "Child 11")             'MiFi End
                .Add(12, 5, "Powershot A1400")             'Camera Start
                .Add(13, 5, "Powershot A1300")             'Camera End
            End With
    
            Return table
        End Function
    
        Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Front View
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 'Back View
    
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 'Open Simulator
            If Me.ComboBox2.ValueMember = "Emulator 1" Then System.Diagnostics.Process.Start("E:\Emulator Tool Project\Blackberry Sim\9000att\fledge.exe")
        End Sub
    
        Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click 'Open Manual
    
        End Sub
    End Class
    This is what I have so far I tried adding what I thought would be the proper code for button 3 but when clicking on it nothing happens. Do I need to add another binding source from the child to button?
    Quote Originally Posted by Mattw11486 View Post
    Actually I just figured it out and I was making it harder then it even had to be.

    Code:
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 'Open Simulator
            If ComboBox2.Text = "Office Jet Pro 8600" Then System.Diagnostics.Process.Start("E:\Emulator Tool Project\Printer Sim\8600\8600 display.png")
            If ComboBox2.Text = "Emulator 1" Then System.Diagnostics.Process.Start("E:\Emulator Tool Project\Blackberry Sim\9000att\fledge.exe")
            If ComboBox2.Text = "Emulator 2" Then System.Diagnostics.Process.Start("E:\Emulator Tool Project\Blackberry Sim\9780tmobile\fledge.exe")
        End Sub
    Why not add another column to the child table that contains the path of the manual? You then don't need any conditional code at all. If you were to name that column "ManualPath" and assign that name to the ValueMember of ComboBox2 then you Button3 Click event handler reduces to this:
    Code:
    Process.Start(CStr(ComboBox2.SelectedValue))

  6. #46
    Addicted Member
    Join Date
    Mar 2016
    Posts
    255

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

    Hi,
    I managed to use binding source for combobox as you illustrated. However, I get an error in the following example:
    Dim SelectString As String = "[customer ID]=" + customerID
    Dim OrderString As String = "[order ID] DESC"

    Dim BS2 As New BindingSource
    BS2.DataSource = DS.order.[Select](SelectString, OrderString)

    CB_ordernumber.DataSource = BS2
    CB_ordernumber.DisplayMember = "customer"
    'CB_ordernumber.ValueMember = " order ID" <<<< I get the error message here "can't bind to the value member

    What is wrong? it works in another case without the select string

  7. #47

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

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

    Quote Originally Posted by smktec View Post
    Hi,
    I managed to use binding source for combobox as you illustrated. However, I get an error in the following example:
    Dim SelectString As String = "[customer ID]=" + customerID
    Dim OrderString As String = "[order ID] DESC"

    Dim BS2 As New BindingSource
    BS2.DataSource = DS.order.[Select](SelectString, OrderString)

    CB_ordernumber.DataSource = BS2
    CB_ordernumber.DisplayMember = "customer"
    'CB_ordernumber.ValueMember = " order ID" <<<< I get the error message here "can't bind to the value member

    What is wrong? it works in another case without the select string
    That is the very antithesis of what I've shown here. The whole point is that you DON'T need any code to filter the child data; it happens automatically. Your question really has nothing at all to do with the topic of this thread. If you had done what I demonstrated then it would have worked. If you don't want to do as I demonstrated then that's fine but please start your own thread to ask questions about what you are doing.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width