我有一个使用 BindingSource 的 DataGridView。这个BindingSource指向一个只读属性,它是一个集合department
对象。当我以编程方式向属性添加部门时,我无法让 DataGridView 重绘自身并显示新部门。
如果我设置断点,我会验证该属性是否有新的部门;但 DataGridView 不绘制它。如果我离开该选项卡,然后返回,DataGridView 将显示新部门。
我的问题本质上是,当我导航离开然后返回 DataGridview 时,如何以编程方式触发发生的情况,强制其重绘(或强制其 BindingSource 更新)?我已经尝试了以下所有方法:
AffectedDepartmentsBindingSource.CancelEdit()
AffectedDepartmentsBindingSource.EndEdit()
AffectedDepartmentsBindingSource.ResetBindings(False)
AffectedDepartmentsBindingSource.ResetBindings(True)
AffectedDepartmentsDataGridView.Refresh()
AffectedDepartmentsDataGridView.Update()
AffectedDepartmentsDataGridView.Hide()
AffectedDepartmentsDataGridView.Show()
这是属性:
Public ReadOnly Property affectedDepartments As SortableBindingList(Of Department)
Get
Dim uniqueDeptIds As New ArrayList
Dim _affectedDepartments As New SortableBindingList(Of Department)
'Go through products
For Each p As product In products.Where(Function(pr) pr.isNotACopy)
If Not uniqueDeptIds.Contains(p.cs_dept_id) Then
uniqueDeptIds.Add(p.cs_dept_id)
Dim d As New Department With {
.schedule = Me,
.name = p.department,
.cs_dept_id = p.cs_dept_id
}
_affectedDepartments.Add(d)
End If
Next
'Add temp departments
If _tempDepartment IsNot Nothing Then
_affectedDepartments.Add(_tempDepartment)
End If
Return _affectedDepartments
End Get
End Property
像这样:
Me.DataGridView1.Invalidate()
Edit
您看不到添加的项目的原因是因为您每次都创建一个新实例。
你应该这样做:
Public ReadOnly Property affectedDepartments As SortableBindingList(Of Department)
Get
Dim uniqueDeptIds As New ArrayList
If (_affectedDepartments Is Nothing) Then
_affectedDepartments = New SortableBindingList(Of Department)
End If
'Go through products
For Each p As product In products.Where(Function(pr) pr.isNotACopy)
If Not uniqueDeptIds.Contains(p.cs_dept_id) Then
uniqueDeptIds.Add(p.cs_dept_id)
Dim d As New Department With {
.schedule = Me,
.name = p.department,
.cs_dept_id = p.cs_dept_id
}
_affectedDepartments.Add(d)
End If
Next
'Add temp departments
If _tempDepartment IsNot Nothing Then
_affectedDepartments.Add(_tempDepartment)
End If
Return _affectedDepartments
End Get
End Property
Private _affectedDepartments As SortableBindingList(Of Department)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)