如何强制DataGridView重绘? [英] How can I force a DataGridView to redraw?
问题描述
我有一个使用BindingSource的DataGridView.该BindingSource指向一个只读属性,该属性是部门
对象的集合.当我以编程方式向属性中添加部门时,我无法使DataGridView重新绘制自身并显示新部门.
I have a DataGridView which uses a BindingSource. This BindingSource points to a read-only property, which is a collection of department
objects. When I programmatically add a department to the property, I am having trouble getting the DataGridView to redraw itself + show the new department.
如果我设置了一个断点,则确认该属性具有新部门;否则,请执行以下操作.但是DataGridView不会绘制它.如果我离开该选项卡,然后再返回,则DataGridView将显示新部门.
If I set a breakpoint, I verify that the property has the new department; yet the DataGridView does not draw it. If I navigate away from the tab, and then back, the DataGridView displays the new department.
我的问题本质上是,当我离开并返回DataGridview时,如何以编程方式触发发生的任何事情,以迫使其重绘(或迫使其BindingSource更新)?我已经尝试了以下所有方法:
My question is essentially, how can I programmatically trigger whatever happens when I navigate away from and then back to the DataGridview, to force it to redraw (or force its BindingSource to update)? I've tried all the following:
AffectedDepartmentsBindingSource.CancelEdit()
AffectedDepartmentsBindingSource.EndEdit()
AffectedDepartmentsBindingSource.ResetBindings(False)
AffectedDepartmentsBindingSource.ResetBindings(True)
AffectedDepartmentsDataGridView.Refresh()
AffectedDepartmentsDataGridView.Update()
AffectedDepartmentsDataGridView.Hide()
AffectedDepartmentsDataGridView.Show()
这里是属性:
Here's the property:
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()
修改
之所以看不到添加的项目,是因为每次都创建一个新实例.
The reason why you don't see the added items is because you create a new instance each time.
您应该这样做:
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)
这篇关于如何强制DataGridView重绘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!