Question : VB.NET - Databinding and Combo UserControl

Hi All,

I want to know the best want to design the databinding functionality in my usercontrol.

The usercontrol itself is made from a textbox and a grid. The grid will be displayed to the user and show them a list of relevant fields from the database (In this case Artist names like madonna). The user basically selects something out of the usercontrol's grid, this will then change the 'ID' and 'Display' properties of the usercontrol. The usercontrol's ID property is below:

        <Bindable(True)>,
        Property ID() As Integer
            Get
                Return m_iID
            End Get
            Set(ByVal value As Integer)
                m_iID = value
                'refresh the data binding
                RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("ID"))
            End Set
        End Property

(The Display property is exactly the same except it changes the textbox's .text property instead of a private variable)

On the form, i have a usercontrol, a Forms.Bindingsource and a populated 'Product' class (with a valid Artist and ArtistID field populated i.e. 123, Madonna). The user control has its 'ID' and 'Display' fields databound to the Datasource via the ArtistID and Artist fields respectivly.

The product class is then set as the BindingSource's datasource. At this point the ID and Display properties of the usercontrol are updated to match the product class that has been binded to the datasource (ID = 123 and Display = Madonna)

All of this is fine, however when i select a new artist from the UserControl (i.e 456, Michael Jackson), the following code is run in the usercontrol:

Me.ID = oSelectedItem.ID '(456)
Me.Display = oSelectedItem.Description '(Michael Jackson)
(oSelectedItem is simply what the user has selected from a list of other artists)

At this point, the 'ArtistID' field WILL change in the Product class (mapped to the ID field in the usercontrol and will equal 456), however the 'Artist' property will remain unaffected (mapped to the display field in the usercontrol, this will remain as 'Madonna' and NOT 'Michael Jackson').

At this point, the usercontrol will now be displaying the new artist name ('Michael Jackson'), if we leave the usercontrol the old artist name is restored from the product object and placed back in the usercontrol ('Madonna' is restored)

What am i doing wrong? Its almost like only the first property change is taking effect and the second line that updates the display is being ignored.

I'm using VB.NET 2005.

Answer : VB.NET - Databinding and Combo UserControl

There are 2 things you could do then.  One is to instead of directly editing the fields you instead change the datasource of the bindingsource to the selected object.  If the selected object is unavailable you have to utilize control.focus after each field change when you are editing bound fields in background code.

Please let me know if that is not clear.
Random Solutions  
 
programming4us programming4us