首页 文章

Binded DataGridView仅更新当前选定的行

提问于
浏览
1

我正在努力让我的DataGridView控件自动更新我的对象模型中的对象的值 . 我有对象数组,我通过以下代码绑定到我的DataGridView

Dim bs As BindingSource = New BindingSource()
    bs.DataSource = aryJoints
    DataGridJoints.DataSource = bs

我使用INotifyPropertyChanged在对象模型中引发事件 . 以下是我的“公共 class 联合”

Implements System.ComponentModel.INotifyPropertyChanged
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

在GoalPosition属性中,我有以下内容:

Private _GoalPosition As Double

    Property GoalPosition() As Double

        Get
            Return _GoalPosition
        End Get

        Set(ByVal Value As Double)
            _GoalPosition = Value

            RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("GoalPosition"))

        End Set

    End Property

如果我从相同的表单或其他形式更新属性aryJoints(0).GoalPosition,DataGridView只会在选择该行时更新DataGridView中的GoalPosition字段 . 如果我在另一行上然后移动到有更改的行,则该行将使用最新值进行更新 .

根据我的理解,一旦我对正在引发的事件进行了适当的绑定,任何单元格都应该使用新值自动更新 . 我不应该在那个单元格中,或者为表单选择该行来反映更改 .

如果我调用DataGridJoints.Refresh(),DataGridView会更新所有值 . 但是创建绑定数据源的重点是避免调用.refresh . 将有许多表单和操作从不同的地方更新对象模型,我需要各种表单上的DataGridViews自动更新自己 .

我错过了什么步骤或做错了什么?

1 回答

  • 0

    正如上面的评论中所建议的那样,尝试切换到BindingList而不是使用绑定数组 . 我测试了这个解决方案并且有效 . 当对象模型更新了值时,DataGridView会根据需要更新任何行 . 所以我将在代码中实现这个改变:)

相关问题