Two Way Data Binding of Combo Box VB.Net

Viewed 229

I am trying to get the two-way combo box binding work. I have a class which serves as source for combo box and i bind the selected item/value of the combo box to data table and the data table is bound to data grid view with binding source. It works but not as i expect it to be. I want to achieve two things.

  1. Update the Data table Grid view as soon as selection changes in the combo box (force update of data)
  2. Add binding to Age column

Here is my code. What is the right way to do. Any help would be highly appreciated. Thanks

Imports System.ComponentModel
Imports System.Runtime.CompilerServices

Public Class Form1
    Implements INotifyPropertyChanged
    Public BS As New BindingSource
    Public ContactBL As BindingList(Of Contacts)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim contacts As New List(Of Contacts) From {
        New Contacts() With {.FirstName = "Name1", .LastName = "LName1", .Age = 50},
        New Contacts() With {.FirstName = "Name2", .LastName = "LName2", .Age = 39},
        New Contacts() With {.FirstName = "Name3", .LastName = "LName3", .Age = 30},
        New Contacts() With {.FirstName = "Name4", .LastName = "LName4", .Age = 66}}

        ContactBL = New BindingList(Of Contacts)(contacts)

        ComboBox1.DataSource = ContactBL
        ComboBox1.DisplayMember = "FirstName"
        ComboBox1.ValueMember = "LastName"


        Dim table As New DataTable("Tab1")
        table.Columns.Add("FirstName", GetType(String))
        table.Columns.Add("LastName", GetType(String))
        table.Columns.Add("Age", GetType(Integer))
        table.Rows.Add("Name3", "LName3", 30)
        table.Rows.Add("Name2", "LName2", 39)

        BS.DataSource = table
        DataGridView1.DataSource = BS

        ComboBox1.DataBindings.Add("SelectedValue", BS, "LastName", True, DataSourceUpdateMode.OnPropertyChanged)
        ComboBox1.DataBindings.Add("Text", BS, "FirstName", True, DataSourceUpdateMode.OnPropertyChanged)

        DataGridView2.DataSource = ContactBL

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim nr = CType(ComboBox1.SelectedItem, Contacts)

        Dim NewRow = DirectCast(BS.AddNew(), DataRowView)
        NewRow("FirstName") = nr.FirstName
        NewRow("LastName") = nr.LastName
        NewRow("Age") = nr.Age
        BS.EndEdit()


    End Sub


    Private _FirstName As String
    Public Property FirstName() As String
        Get
            Return _FirstName
        End Get
        Set(ByVal value As String)
            _FirstName = value
            OnPropertyChanged()
        End Set
    End Property

    Private _LastName As String
    Public Property LastName() As String
        Get
            Return _LastName
        End Get
        Set(ByVal value As String)
            _LastName = value
            OnPropertyChanged()
        End Set
    End Property

    Private _Age As Integer
    Public Property Age() As Integer
        Get
            Return _Age
        End Get
        Set(ByVal value As Integer)
            _Age = value
            OnPropertyChanged()
        End Set
    End Property


    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    Protected Overridable Sub OnPropertyChanged(<CallerMemberName>
    Optional memberName As String = Nothing)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(memberName))
    End Sub

    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

End Class


Public Class Contacts

    Implements INotifyPropertyChanged
    Private _firstName As String
    Private _lastName As String
    Private _Age As Integer

    Public Property ContactId() As Integer

    Public Property FirstName() As String
        Get
            Return _firstName
        End Get
        Set
            _firstName = Value
            OnPropertyChanged()
        End Set
    End Property
    Public Property LastName() As String
        Get
            Return _lastName
        End Get
        Set
            _lastName = Value
            OnPropertyChanged()
        End Set
    End Property
    Public Property Age() As Integer
        Get
            Return _Age
        End Get
        Set
            _Age = Value
            OnPropertyChanged()
        End Set
    End Property
    Public ReadOnly Property FullName() As String
        Get
            Return $"{FirstName} {LastName}"
        End Get
    End Property
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    Protected Overridable Sub OnPropertyChanged(<CallerMemberName>
    Optional memberName As String = Nothing)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(memberName))
    End Sub

End Class

Thanks

0 Answers
Related