locked
Update List on Loaded Forms RRS feed

  • Question

  • Hi Everyone,

    I have a User Class and 2 Forms with One Datagrid and One Button & TextBox to Save User.

    Both Frm as Loaded. Datagrid of Both Forms are Bound with mUser.GetUsers But When I Enter Name of New User and Press Save button Datagrid of same form updates Automatically. But Not Update the Grid of 2nd Form. My Question is How to Automatically Update on Both foms.

    Public Class User

        Implements INotifyPropertyChanged
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
        Public ListChangeEvent As New ListChangedEventArgs(ListChangedType.Reset, -1)
        Private Access As New DBControl
        Private CurrentRecord As Integer = 0
        Private _UserID As Integer
        Private _UseryName As String
        Public _UserList As New List(Of User)

        Public Property UserID() As Integer
            Get
                Return _UserID
            End Get
            Set(ByVal value As Integer)
                _UserID = value
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(NameOf(Me.UserID)))
            End Set
        End Property

        Public Property UserName() As String
            Get
                Return _UseryName
            End Get
            Set(ByVal value As String)
                If IsNumeric(value) = True Then MsgBox("Category Could Not Change ") : Exit Property
                _UseryName = value
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(NameOf(Me.UserName)))
            End Set
        End Property

        Public Overloads Sub SaveRecord()
            Access.AddParam("@d1", Me.UserName)
            Access.ExecQuery("Insert into UsersInfo ([UserName]) " &
                                     "Values (@d1);")

            If Not String.IsNullOrEmpty(Access.Exception) Then MsgBox(Access.Exception) : Exit Sub
            If Access.NoErrors(True) = False Then Exit Sub
            XtraMessageBox.Show("Saved Successfully")


            OnListChanged(ListChangeEvent)

        End Sub


        Protected Overridable Sub OnListChanged(ByVal ev As ListChangedEventArgs)
            GetUsers()
        End Sub

        Public Function GetUsers()
            ' QUERY USER
            Access.ExecQuery("SELECT * FROM UsersInfo")

            ' REPORT & ABORT ON ERRORS OR NO RECORDS FOUND
            If Access.NoErrors(True) = False OrElse Access.RecordCount < 1 Then Exit Function

            ' GET FIRST ROW FOUND
            Dim r As DataRow = Access.DBDT.Rows(0)

            If _UserList IsNot Nothing Then _UserList.Clear()
            For Each r In Access.DBDT.Rows
                ' POPULATE TEXTBOXES WITH DATA
                _UserList.Add(New User With {
                             .UserID = Convert.ToInt32(r("ID")),
                             .UserName = r("UserName").ToString
                             })

            Next

            Return _UserList
        End Function




    End Class

    Monday, November 16, 2020 3:00 PM

All replies

  • Hello,

    First off I don't have time to write up a code sample. What I have time for is to correct how you (with no disrespect) how you have setup the user class.

    The user class should only have properties in this case along with using a slightly different pattern for INotifyPropertyChanged.

    Note, only properties along with how INotifyPropertyChanged is done in regards to OnPropertyChanged() in the Set then in OnPropertyChanged CallerMemberName knows the property name.

    Imports System.ComponentModel
    Imports System.Runtime.CompilerServices
    
    Public Class User
        Implements INotifyPropertyChanged
    
        Private _name As String
        Private _id As Integer
    
        Public Property Id() As Integer
            Get
                Return _id
            End Get
            Set
                _id = Value
                OnPropertyChanged()
            End Set
        End Property
    
        Public Property Name() As String
            Get
                Return _name
            End Get
            Set
                _name = 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
    End Class
    

    The ListChanged event and data operations should not be in the User class. Data operations can be in a form but much better in a data class using shared keyword on methods and the ListChanged implementation can be in the same data class or in a form but in a data class makes more sense.

    Also look at using a BindingList(Of User) coupled with a BindingSource, the BindingList becomes the DataSource to the BindingSource while the BindingSource becomes the DataSource of the DataGridView controls.

    To get an idea of using a BindingList/BindingSource see the following project.

    I'm at work and to give you a complete working code sample is not possible.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    My GitHub code samples
    GitHub page

    Monday, November 16, 2020 3:42 PM