Hamada
Hamada

Reputation: 517

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!?

Upvotes: 2

Views: 73

Answers (2)

Caius Jard
Caius Jard

Reputation: 74615

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)

Upvotes: 2

Hamada
Hamada

Reputation: 517

I found a solution, but if anybody has another solution please share it with us, I used ".ToTable" at last of the data view.

    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.ToTable)

    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.ToTable)

and I used the next code:

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

xdv.RowFilter = "AccType='Final'"
Call xCLS.MyCodes_CboFill_Dv(Me.CboAccFinal, "AccName", "AccID", xdv.ToTable)

can anybody give us any comment about which solution is better and what the difference?

Upvotes: 1

Related Questions