Combo-box data binding to a class property.

Respondida Combo-box data binding to a class property.

  • martes, 14 de agosto de 2012 7:40
     
      Tiene código

    Hey Guys, 

    I have a combo-box which has a data table as its data source.  The combo box is also bound to a class property, because every time the combo's selected value changes, I would want my class's property to reflect this change. This works fine. However, my goal is, whenever I set my classes's property, I would like my combo box to reflect that change too. This link seems to be broken. 

    Here is a sample code snippet

    Private _suburbdatasource As BindingSource
    
        Public Property SuburbDataSource As IList
            Get
                Return _suburbdatasource
            End Get
            Set(ByVal value As IList)
                Select Case True
                    Case value.GetType.Name.StartsWith("LinqDataView")
                        With Me.ComboBox1
                            .DataSource = value
                            .ValueMember = "ID"
                            .DisplayMember = "Suburb"
                        End With
                End Select
            End Set
        End Property
    
        Public Function CreateSuburbData() As DataTable
            Dim Suburbs As DataTable = New System.Data.DataTable()
    
            Suburbs.Columns.Add("ID", System.Type.GetType("System.Int32"))
            Suburbs.Columns.Add("Suburb", System.Type.GetType("System.String"))
            Suburbs.Columns.Add("State", System.Type.GetType("System.String"))
            Suburbs.Columns.Add("Postcode", System.Type.GetType("System.String"))
    
            Suburbs.Rows.Add(1, "Essendon", "VIC", "3040")
            Suburbs.Rows.Add(2, "NorthRyde", "NSW", "2122")
            Suburbs.Rows.Add(3, "Chatswood", "NSW", "2124")
            Suburbs.Rows.Add(4, "Cheltanham", "NSW", "2523")
            Suburbs.Rows.Add(5, "Hornsby", "NSW", "3212")
            Suburbs.Rows.Add(6, "Gladesville", "VIC", "6543")
            Suburbs.Rows.Add(7, ".", ".", ".")
    
            Return Suburbs
        End Function
    
        Private Sub PopulateCombo()
            Dim mydatatable As DataTable = CreateSuburbData()
            Me.SuburbDataSource = mydatatable.AsDataView
        End Sub
    
        Private Sub JanusForm_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
           
            PopulateCombo()
           
            Dim suburb As SuburbClass = New SuburbClass
            Me.ComboBox1.DataBindings.Add("SelectedValue", suburb, "MySuburb", True, DataSourceUpdateMode.OnPropertyChanged)
    
            'The below line displays the correct record i.e Cheltanham in the combo box.
            'Also when the below line executes, suburb.MySuburb has the value Cheltanham
            Me.ComboBox1.SelectedValue = 4
    
            'Since the combo box is data bound, I would except that when the below line executes, 
            'Me.ComboBox1.SelectedValue is set to 4 and the data that the combobox displays is Cheltanham. But none of those things happen here.
            suburb.MySuburb = 4
                   
        End Sub

    Public Class SuburbClass
        Implements INotifyPropertyChanged
    
        Private suburb As Integer
    
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    
        Public Property MySuburb As Integer
            Get
                Return Me.suburb
            End Get
            Set(ByVal value As Integer)
                Me.suburb = value
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(MySuburb))
            End Set
        End Property
    
        Protected Sub OnPropertyChanged(ByVal name As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
        End Sub
    End Class

    I know this should be relatively simple, abut I just cant get my head around this issue. Any help will be appreciated.

    Thanks

    Lavanya


Todas las respuestas

  • miércoles, 15 de agosto de 2012 7:06
    Moderador
     
     Respondida Tiene código

    Hi lavanya,

    Welcome to the MSDN forum.

    You can solve your issue just change:

    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(MySuburb))

    To :

    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("MySuburb"))

    The property name needs to be a string type.

    Hope this helps.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

    • Marcado como respuesta SIT-Laavanya jueves, 16 de agosto de 2012 0:02
    •  
  • jueves, 16 de agosto de 2012 0:02
     
     

    Aaahhhh, yes obviously. Thanks so much for that Mark Liu-lxf.

    Regards,

    Laavanya