Search code examples
vb.netdatatabledataview

Using sepirated Dataviews with one Datatable for filling two combobox


I have one Datatable and I have two Dataviews, I wanted to use each Dataview to fill a Combobox as next code:

        Dim xDv_Parents As DataView = MyVar_Dt_AllAccounts.DefaultView
        xDv_Parents.RowFilter = "AccType='Main'"
        Call xCLS.MyCodes_CboFill_Dv(Me.CboAccParent, "AccName", "AccID", xDv_Parents)

        Dim xDv_Final As DataView = MyVar_Dt_AllAccounts.DefaultView
        xDv_Final.RowFilter = "AccType='Final'"
        Call xCLS.MyCodes_CboFill_Dv(Me.CboAccFinal, "AccName", "AccID", xDv_Final)

but when I debug the app it gives me the same content in two Comboboxs according the last Dataview the named is (xDv_Final).

I tried to use one Dataview with different row filter but it wasn't a success. what's the problem exactly here please!?


Solution

  • It's because you reused the same view for both comboboxes. All you did was establish new variables pointing to the same object in memory. First you had this:

    enter image description here

    Then you made another variable, same object:

        Dim xDv_Parents As DataView = MyVar_Dt_AllAccounts.DefaultView
    

    enter image description here

    Then you set a rowfilter:

        xDv_Parents.RowFilter = "AccType='Main'"
    

    enter image description here

    Then you told one combo to use it, so now you have a combo pointing to it too:

        Call xCLS.MyCodes_CboFill_Dv(Me.CboAccParent, "AccName", "AccID", xDv_Parents)
    

    enter image description here

    Then you made another variable, also pointing to it, and overwrote the rowfilter you just set:

        Dim xDv_Final As DataView = MyVar_Dt_AllAccounts.DefaultView
        xDv_Final.RowFilter = "AccType='Final'"
    

    enter image description here

    Finally you attached another combo to the same view:

        Call xCLS.MyCodes_CboFill_Dv(Me.CboAccFinal, "AccName", "AccID", xDv_Final)
    

    enter image description here

    This is just how variables in .net programming languages work. Unless you use New you're just attaching new variables to existing object instances

    What you should have done was:

        Dim xDv_Parents = new DataView(MyVar_Dt_AllAccounts)
        xDv_Parents.RowFilter = "AccType='Main'"
        xCLS.MyCodes_CboFill_Dv(Me.CboAccParent, "AccName", "AccID", xDv_Parents)
    
        Dim xDv_Final = New DataView(MyVar_Dt_AllAccounts.DefaultView)
        xDv_Final.RowFilter = "AccType='Final'"
        xCLS.MyCodes_CboFill_Dv(Me.CboAccFinal, "AccName", "AccID", xDv_Final)