Hello @Hobbyist_programmer
Even though you have an accepted solution I'd like to provide another solution.
- Rather than a shared field use a non-shared field unless you want all existing rows to change.
- There is really no need to have a wrapper class simply for the BindingList, instead use the Setting class assigned to a BindingSource in the form. The class should really only have properties and INotifyPropertyChanged event and OnPropertyChanged
Setting class
Read the comments
Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Namespace Classes
Public Class Setting
Implements INotifyPropertyChanged
Public Sub New(Optional language As String = "English")
languageValue = language
End Sub
''' <summary>
''' Shared modifier means all instances are affected,
''' remove Shared if that is not what you want.
''' </summary>
Public languageValue As String = ""
Public Property Language() As String
Get
Return languageValue
End Get
Set
languageValue = Value
'
' Make sure to implement the line below
'
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
End Namespace
Form code
Imports System.ComponentModel
Imports BindingWithoutRefreshing.Classes
Public Class Form1
Public SettingsBindingSource As New BindingSource
Public SettingsBindingList As New BindingList(Of Setting)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView2.AutoGenerateColumns = False
SettingsBindingList = New BindingList(Of Setting)(New List(Of Setting))
SettingsBindingSource.DataSource = New BindingSource(SettingsBindingList, Nothing)
DataGridView2.DataSource = SettingsBindingSource
LanguagesComboBox.DataSource = New List(Of String) From {"English", "French", "Spanish"}
End Sub
Private Sub AddNewSettingButton_Click(sender As Object, e As EventArgs) _
Handles AddNewSettingButton.Click
SettingsBindingSource.Add(New Setting(LanguagesComboBox.Text))
SettingsBindingSource.MoveLast()
End Sub
Private Sub ChangeLanguageButton_Click(sender As Object, e As EventArgs) _
Handles ChangeLanguageButton.Click
If SettingsBindingSource.Current IsNot Nothing Then
CType(SettingsBindingSource.Current, Setting).Language = LanguagesComboBox.Text
End If
End Sub
Private Sub DataGridView2_DefaultValuesNeeded(sender As Object, e As DataGridViewRowEventArgs) _
Handles DataGridView2.DefaultValuesNeeded
e.Row.Cells(0).Value = LanguagesComboBox.Text
End Sub
End Class
Full source GitHub repository