locked
Problem changing value in Bindingsource Datatable RRS feed

  • Question

  • Hallo 

    I have 3 cascading combo boxes and 3 text boxes, all the values are bound to data table by a binding source. I want to clear/empty the text boxes when i change or select the item in the combo boxes manually my code clears the data table values and clears the textbox but combobox item is not changing. it stays the same why?.

    Imports System.IO
    Public Class Form1
        Public BS As New BindingSource
        Public ds2 As New DataSet
        Public XML_DS As New DataSet
        Public ds As New DataSet
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Call create_DAT()
    
            XML_DS.ReadXml("XML\L1.xml")
    
            BS.DataSource = ds2.Tables("Master")
            dgv.DataSource = BS
    
            cb1.DataSource = XML_DS.Tables("L1")
            cb1.DisplayMember = "eNAME"
            cb1.ValueMember = "eID"
            cb1.DataBindings.Add("Text", BS, "Master")
    
            cb2.DataSource = XML_DS.Tables("L2")
            cb2.DisplayMember = "eTYPE"
            cb2.ValueMember = "tID"
            cb2.DataBindings.Add("Text", BS, "Child")
    
            cb3.DataSource = XML_DS.Tables("L3")
            cb3.DisplayMember = "eMAT"
            cb3.ValueMember = "mID"
            cb3.DataBindings.Add("Text", BS, "Grand_Child")
    
    
            tb1.DataBindings.Add("Text", BS, "Text1", True, DataSourceUpdateMode.OnPropertyChanged, String.Empty)
            tb2.DataBindings.Add("Text", BS, "Text2", True, DataSourceUpdateMode.OnPropertyChanged, String.Empty)
            tb3.DataBindings.Add("Text", BS, "Text3", True, DataSourceUpdateMode.OnPropertyChanged, String.Empty)
    
            AddHandler BS.BindingComplete, AddressOf BindingComplete
    
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Call add()
        End Sub
    
        Private Sub cb1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cb1.SelectedValueChanged
    
            cb2.DataSource = New DataView(XML_DS.Tables("L2"), $"eID= '{cb1.SelectedValue}'", "", DataViewRowState.CurrentRows)
    
        End Sub
    
        Private Sub cb2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cb2.SelectedValueChanged
    
            cb3.DataSource = New DataView(XML_DS.Tables("L3"), $"tID= '{cb2.SelectedValue}'", "", DataViewRowState.CurrentRows)
    
        End Sub
    
        Private Sub cb1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles cb1.SelectionChangeCommitted
    
            Dim nRow = DirectCast(BS.Current, DataRowView)
            If Not nRow Is Nothing Then
    
                nRow("Text1") = String.Empty
                nRow("Text2") = String.Empty
                nRow("Text3") = String.Empty
    
            End If
    
            tb1.Text = String.Empty
            tb2.Text = String.Empty
            tb3.Text = String.Empty
    
        End Sub
        Public Sub create_DAT()
    
    
            With ds2
                With .Tables.Add("Master")
                    With .Columns
                        .Add("Master", GetType(String))
                        .Add("Child", GetType(String))
                        .Add("Grand_Child", GetType(String))
                        .Add("Text1", GetType(String))
                        .Add("Text2", GetType(String))
                        .Add("Text3", GetType(String))
                    End With
                End With
            End With
    
    
        End Sub
    
        Public Sub BindingComplete(ByVal sender As Object, ByVal e As BindingCompleteEventArgs)
    
            If e.BindingCompleteContext = BindingCompleteContext.DataSourceUpdate AndAlso e.Exception Is Nothing Then e.Binding.BindingManagerBase.EndCurrentEdit()
    
        End Sub
    
        Public Sub add()
    
    
            Dim cb11 As String = cb1.Text
            Dim cb12 As String = cb2.Text
            Dim cb13 As String = cb3.Text
    
            Dim t1 As String = "xxxx"
    
    
            Dim nRow = DirectCast(BS.AddNew(), DataRowView)
                nRow("Master") = cb11
                nRow("Child") = cb12
            nRow("Grand_Child") = cb13
            nRow("Text1") = t1
            nRow("Text2") = t1
            nRow("Text3") = t1
            BS.EndEdit()
    
        End Sub
    
    End Class
    

    XML

    <?xml version="1.0" encoding="UTF-8"?>
    
    -<dataroot generated="2020-08-15T14:54:41" xmlns:od="urn:schemas-microsoft-com:officedata">
    
    
    -<L1>
    
    <eID>VG</eID>
    
    <eNAME>Vegis</eNAME>
    
    
    -<L2>
    
    <tID>VG.01</tID>
    
    <eID>VG</eID>
    
    <eTYPE>Carrot</eTYPE>
    
    
    -<L3>
    
    <mID>VG.01.1K</mID>
    
    <tID>VG.01</tID>
    
    <eMAT>Caarot 1kg</eMAT>
    
    </L3>
    
    
    -<L3>
    
    <mID>VG.01.2K</mID>
    
    <tID>VG.01</tID>
    
    <eMAT>Caarot 2kg</eMAT>
    
    </L3>
    
    </L2>
    
    
    -<L2>
    
    <tID>VG.02</tID>
    
    <eID>VG</eID>
    
    <eTYPE>Onions</eTYPE>
    
    
    -<L3>
    
    <mID>VG.02.1k</mID>
    
    <tID>VG.02</tID>
    
    <eMAT>Onions 1kg</eMAT>
    
    </L3>
    
    
    -<L3>
    
    <mID>VG.02.2K</mID>
    
    <tID>VG.02</tID>
    
    <eMAT>Onions 2kg</eMAT>
    
    </L3>
    
    </L2>
    
    
    -<L2>
    
    <tID>VG.03</tID>
    
    <eID>VG</eID>
    
    <eTYPE>Potato</eTYPE>
    
    
    -<L3>
    
    <mID>VG.03.1K</mID>
    
    <tID>VG.03</tID>
    
    <eMAT>Potato 1Kg</eMAT>
    
    </L3>
    
    
    -<L3>
    
    <mID>VG.03.2K</mID>
    
    <tID>VG.03</tID>
    
    <eMAT>Potato 2Kg</eMAT>
    
    </L3>
    
    </L2>
    
    </L1>
    
    
    -<L1>
    
    <eID>FR</eID>
    
    <eNAME>Fruits</eNAME>
    
    
    -<L2>
    
    <tID>FR.01</tID>
    
    <eID>FR</eID>
    
    <eTYPE>Apple</eTYPE>
    
    
    -<L3>
    
    <mID>FR.01.F1</mID>
    
    <tID>FR.01</tID>
    
    <eMAT>Apple 1K</eMAT>
    
    </L3>
    
    
    -<L3>
    
    <mID>FR.01.F2</mID>
    
    <tID>FR.01</tID>
    
    <eMAT>Apple 2K</eMAT>
    
    </L3>
    
    </L2>
    
    
    -<L2>
    
    <tID>FR.02</tID>
    
    <eID>FR</eID>
    
    <eTYPE>Banana</eTYPE>
    
    
    -<L3>
    
    <mID>FR.02.B1</mID>
    
    <tID>FR.02</tID>
    
    <eMAT>Banana 1k</eMAT>
    
    </L3>
    
    </L2>
    
    
    -<L2>
    
    <tID>FR.03</tID>
    
    <eID>FR</eID>
    
    <eTYPE>Figs</eTYPE>
    
    
    -<L3>
    
    <mID>FR.03.FI</mID>
    
    <tID>FR.03</tID>
    
    <eMAT>1kg</eMAT>
    
    </L3>
    
    </L2>
    
    </L1>
    
    
    -<L1>
    
    <eID>MT</eID>
    
    <eNAME>Meats</eNAME>
    
    
    -<L2>
    
    <tID>MT.01</tID>
    
    <eID>MT</eID>
    
    <eTYPE>Fish</eTYPE>
    
    
    -<L3>
    
    <mID>MT.01.FI</mID>
    
    <tID>MT.01</tID>
    
    <eMAT>Fish 1k</eMAT>
    
    </L3>
    
    
    -<L3>
    
    <mID>MT.01.FI2</mID>
    
    <tID>MT.01</tID>
    
    <eMAT>Fish 2k</eMAT>
    
    </L3>
    
    </L2>
    
    
    -<L2>
    
    <tID>MT.02</tID>
    
    <eID>MT</eID>
    
    <eTYPE>Chicken</eTYPE>
    
    
    -<L3>
    
    <mID>MT.02.C1</mID>
    
    <tID>MT.02</tID>
    
    <eMAT>1k</eMAT>
    
    </L3>
    
    
    -<L3>
    
    <mID>MT.02.C2</mID>
    
    <tID>MT.02</tID>
    
    <eMAT>2K</eMAT>
    
    </L3>
    
    </L2>
    
    
    -<L2>
    
    <tID>MT.03</tID>
    
    <eID>MT</eID>
    
    <eTYPE>Mutton</eTYPE>
    
    
    -<L3>
    
    <mID>MT.03.M1</mID>
    
    <tID>MT.03</tID>
    
    <eMAT>1K</eMAT>
    
    </L3>
    
    
    -<L3>
    
    <mID>MT.03.M2</mID>
    
    <tID>MT.03</tID>
    
    <eMAT>2K</eMAT>
    
    </L3>
    
    </L2>
    
    </L1>
    
    
    -<L1>
    
    <eID>DR</eID>
    
    <eNAME>Dairy</eNAME>
    
    
    -<L2>
    
    <tID>DR.01</tID>
    
    <eID>DR</eID>
    
    <eTYPE>Milk</eTYPE>
    
    
    -<L3>
    
    <mID>DR.01.MI1</mID>
    
    <tID>DR.01</tID>
    
    <eMAT>1L</eMAT>
    
    </L3>
    
    
    -<L3>
    
    <mID>DR.01.MI2</mID>
    
    <tID>DR.01</tID>
    
    <eMAT>2L</eMAT>
    
    </L3>
    
    </L2>
    
    
    -<L2>
    
    <tID>DR.02</tID>
    
    <eID>DR</eID>
    
    <eTYPE>Curd</eTYPE>
    
    
    -<L3>
    
    <mID>DR.02.C1</mID>
    
    <tID>DR.02</tID>
    
    <eMAT>5 Pack</eMAT>
    
    </L3>
    
    
    -<L3>
    
    <mID>DR.02.C2</mID>
    
    <tID>DR.02</tID>
    
    <eMAT>12 Pack</eMAT>
    
    </L3>
    
    </L2>
    
    
    -<L2>
    
    <tID>DR.03</tID>
    
    <eID>DR</eID>
    
    <eTYPE>Cheese</eTYPE>
    
    
    -<L3>
    
    <mID>DR.03.CH1</mID>
    
    <tID>DR.03</tID>
    
    <eMAT>500g</eMAT>
    
    </L3>
    
    
    -<L3>
    
    <mID>DR.03.CH2</mID>
    
    <tID>DR.03</tID>
    
    <eMAT>1kg</eMAT>
    
    </L3>
    
    </L2>
    
    </L1>
    
    
    -<L1>
    
    <eID>CH</eID>
    
    <eNAME>Chokos</eNAME>
    
    
    -<L2>
    
    <tID>CH.01</tID>
    
    <eID>CH</eID>
    
    <eTYPE>Chocolate1</eTYPE>
    
    
    -<L3>
    
    <mID>CH.01.CC1</mID>
    
    <tID>CH.01</tID>
    
    <eMAT>5pck</eMAT>
    
    </L3>
    
    </L2>
    
    
    -<L2>
    
    <tID>CH.02</tID>
    
    <eID>CH</eID>
    
    <eTYPE>Chocolate2</eTYPE>
    
    
    -<L3>
    
    <mID>CH.02.CC2</mID>
    
    <tID>CH.02</tID>
    
    <eMAT>10pck</eMAT>
    
    </L3>
    
    </L2>
    
    
    -<L2>
    
    <tID>CH.03</tID>
    
    <eID>CH</eID>
    
    <eTYPE>Chocolate3</eTYPE>
    
    
    -<L3>
    
    <mID>CH.02.CC3</mID>
    
    <tID>CH.03</tID>
    
    <eMAT>20pck</eMAT>
    
    </L3>
    
    </L2>
    
    </L1>
    
    
    -<L1>
    
    <eID>CK</eID>
    
    <eNAME>Cakes</eNAME>
    
    
    -<L2>
    
    <tID>CK.01</tID>
    
    <eID>CK</eID>
    
    <eTYPE>Cake1</eTYPE>
    
    
    -<L3>
    
    <mID>CK.01.01</mID>
    
    <tID>CK.01</tID>
    
    <eMAT>200g</eMAT>
    
    </L3>
    
    </L2>
    
    
    -<L2>
    
    <tID>CK.02</tID>
    
    <eID>CK</eID>
    
    <eTYPE>Cake2</eTYPE>
    
    
    -<L3>
    
    <mID>CK.02.01</mID>
    
    <tID>CK.02</tID>
    
    <eMAT>300g</eMAT>
    
    </L3>
    
    </L2>
    
    
    -<L2>
    
    <tID>CK.03</tID>
    
    <eID>CK</eID>
    
    <eTYPE>Cake3</eTYPE>
    
    
    -<L3>
    
    <mID>CK.03.01</mID>
    
    <tID>CK.03</tID>
    
    <eMAT>500g</eMAT>
    
    </L3>
    
    </L2>
    
    </L1>
    
    </dataroot>

    Saturday, August 15, 2020 1:39 PM

Answers

  • Hi Shan1986,

    >>I want to show the the combo box selected values when i select a row from data grid view.

    You can consider using following code to do it.

        Private Sub dgv_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellClick
            If e.RowIndex >= 0 Then
                Dim row As DataGridViewRow = dgv.Rows(e.RowIndex)
                cb1.Text = row.Cells("Master").Value.ToString
                cb2.Text = row.Cells("Child").Value.ToString
                cb3.Text = row.Cells("Grand_Child").Value.ToString
            End If
        End Sub

    Result of my test:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Shan1986 Thursday, August 20, 2020 7:22 PM
    Wednesday, August 19, 2020 1:53 AM

All replies

  • Hello,

    The following is not a solution as it uses DataGridView and a database but a pointer in the right direction. For each level use a BindingSource. Each DataTable needs a relationship set so that when changing something in say the master reflects changes in the child and grandchild controls. Look at the code in this backend class and the front end form (see master form.

    With the above recommendation I would go this route with XML to as it doesn't matter where the data is coming from but instead how you setup things.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, August 15, 2020 2:18 PM
  • Hello,

    The following is not a solution as it uses DataGridView and a database but a pointer in the right direction. For each level use a BindingSource. Each DataTable needs a relationship set so that when changing something in say the master reflects changes in the child and grandchild controls. Look at the code in this backend class and the front end form (see master form.

    With the above recommendation I would go this route with XML to as it doesn't matter where the data is coming from but instead how you setup things.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Hallo Karen, If i understand your answer correctly , you mean for the cascading combo boxes i must use the data relations like in your post? Thanks
    Saturday, August 15, 2020 4:40 PM
  • Hallo,

    I tried also with setting data relations for the cascading combo boxes but the problem is same. If i disable following code all cascading and combo box selection works fine with both methods. Any idea how to clear these text boxes with any other way?

    Private Sub cb1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles cb1.SelectionChangeCommitted
    
            Dim nRow = DirectCast(BS.Current, DataRowView)
            If Not nRow Is Nothing Then
    
                nRow("Text1") = String.Empty
                nRow("Text2") = String.Empty
                nRow("Text3") = String.Empty
    
            End If
    
            tb1.Text = String.Empty
            tb2.Text = String.Empty
            tb3.Text = String.Empty
    
        End Sub

    Thanks

    Sunday, August 16, 2020 10:22 AM
  • Hi Shan1986,

    We’re doing research on this issue. It might take some time before we get back to you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 17, 2020 9:49 AM
  • Hi Shan1986,

    We’re doing research on this issue. It might take some time before we get back to you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Hi Xingyu. Thanks 
    Monday, August 17, 2020 7:41 PM
  • Hi Shan1986,

    It seems that you don't need to add data binding  to your combobox 'Text' property.

            cb1.DataSource = XML_DS.Tables("L1")
            cb1.DisplayMember = "eNAME"
            cb1.ValueMember = "eID"
            'cb1.DataBindings.Add("Text", BS, "Master")
    
            cb2.DataSource = XML_DS.Tables("L2")
            cb2.DisplayMember = "eTYPE"
            cb2.ValueMember = "tID"
            'cb2.DataBindings.Add("Text", BS, "Child")
    
            cb3.DataSource = XML_DS.Tables("L3")
            cb3.DisplayMember = "eMAT"
            cb3.ValueMember = "mID"
            'cb3.DataBindings.Add("Text", BS, "Grand_Child")

    Result:

    Hope it could be helpful.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 18, 2020 3:01 AM
  • Hi Shan1986,

    It seems that you don't need to add data binding  to your combobox 'Text' property.

            cb1.DataSource = XML_DS.Tables("L1")
            cb1.DisplayMember = "eNAME"
            cb1.ValueMember = "eID"
            'cb1.DataBindings.Add("Text", BS, "Master")
    
            cb2.DataSource = XML_DS.Tables("L2")
            cb2.DisplayMember = "eTYPE"
            cb2.ValueMember = "tID"
            'cb2.DataBindings.Add("Text", BS, "Child")
    
            cb3.DataSource = XML_DS.Tables("L3")
            cb3.DisplayMember = "eMAT"
            cb3.ValueMember = "mID"
            'cb3.DataBindings.Add("Text", BS, "Grand_Child")

    Result:

    Hope it could be helpful.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Hallo Xingyu. I want to show the the combo box selected values when i select a row from data grid view so i cant remove the combo box bindings.
    Tuesday, August 18, 2020 6:24 AM
  • Hi Shan1986,

    >>I want to show the the combo box selected values when i select a row from data grid view.

    You can consider using following code to do it.

        Private Sub dgv_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellClick
            If e.RowIndex >= 0 Then
                Dim row As DataGridViewRow = dgv.Rows(e.RowIndex)
                cb1.Text = row.Cells("Master").Value.ToString
                cb2.Text = row.Cells("Child").Value.ToString
                cb3.Text = row.Cells("Grand_Child").Value.ToString
            End If
        End Sub

    Result of my test:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Shan1986 Thursday, August 20, 2020 7:22 PM
    Wednesday, August 19, 2020 1:53 AM