Databinding not refreshing

TheMightyAtom

Member
Joined
Jul 19, 2008
Messages
5
Programming Experience
1-3
Hi
I created a simple Person Class, made a couple of people objects, added them to a BindingList and used the bindinglist as the datagridview's datasource. So far, so good.

If I make any changes to a cell and tab out of the cell I have raised the ListChanged event but the changes are not seen in the textbox that are bound to the same datasource.

Can anyone please tell me where I have gone wrong?

If I move to a different row and then back the changes are seen.



VB.NET:
Expand Collapse Copy
Option Explicit On
Option Strict On
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms

Public Class NoddyLad2


   ' Declare a new BindingListOfT with the Part business object.
   Private WithEvents listOfPeople As BindingList(Of Person)
   Private bmb As BindingManagerBase

   Private Sub NoddyLad2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      loadRecordData()
      loadcontrols()
      addDataBindings()
      buildgrid()
   End Sub


   Private Sub loadRecordData()
      listOfPeople = New BindingList(Of Person)
      listOfPeople.Add(New Person("John", "Rogue"))
      listOfPeople.Add(New Person("Mike", "Tubricks"))

      ' Raise ListChanged events when object edited.
      listOfPeople.RaiseListChangedEvents = True
      listOfPeople.AllowEdit = True

   End Sub
   Private Sub loadcontrols()
      dgvPeople.DataSource = listOfPeople
   End Sub
   Private Sub addDataBindings()
      txtForename.DataBindings.Add("Text", listOfPeople, "Forename")
      txtSurname.DataBindings.Add("Text", listOfPeople, "Surname")

      bmb = Me.BindingContext(listOfPeople)
      bmb.Position = 0
   End Sub
   Private Sub buildgrid()
      dgvPeople.EditMode = DataGridViewEditMode.EditOnEnter
      dgvPeople.AllowUserToAddRows = False
      'For Each row As DataGridViewRow In dgvPeople.Rows
      '   row.ReadOnly = True
      'Next

   End Sub
   Private Sub dgvPeople_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPeople.CellValueChanged
      Debug.WriteLine("Cell value changed")

      If bmb IsNot Nothing Then
         Dim currentRow As Integer = dgvPeople.CurrentCell.RowIndex
         bmb.Position = currentRow
         bmb.EndCurrentEdit()
      End If
      
      listOfPeople_ListChanged(Me, New ListChangedEventArgs(ListChangedType.ItemChanged, 0))
   End Sub
   Private Sub listOfPeople_ListChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs) Handles listOfPeople.ListChanged
      Debug.WriteLine("List changed")

      If bmb IsNot Nothing Then
         Dim currentRow As Integer = dgvPeople.CurrentCell.RowIndex
         bmb.Position = currentRow
         bmb.EndCurrentEdit()
      End If

   End Sub

   ' A simple business object for example purposes.
   Public Class Person
      Private _Forename As String
      Private _Surname As String

      Public Sub New()
      End Sub

      Public Sub New(ByVal Forename As String, _
                     ByVal Surname As String)
         _Forename = Forename
         _Surname = Surname

      End Sub


      Public Property Forename() As String
         Get
            Return _Forename
         End Get
         Set(ByVal value As String)
            _Forename = value
            Debug.WriteLine("Forename changed to: " + value)
         End Set
      End Property

      Public Property Surname() As String
         Get
            Return _Surname
         End Get
         Set(ByVal value As String)
            _Surname = value
            Debug.WriteLine("Surnamename changed to: " + value)
         End Set
      End Property
   End Class


   
End Class
 
Last edited:
Solution

Very easy fix. I just called the bindings to reset to get the new values which in turn fires the listHasChanged event



VB.NET:
Expand Collapse Copy
Private Sub dgvPeople_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPeople.CellValueChanged

      listOfPeople.ResetBindings()

   End Sub
 
Back
Top