DataGridView控件未按预期显示更改

时间:2013-12-03 11:06:36

标签: vb.net data-binding datagridview

我正在尝试学习如何将对象数据绑定到datagridview(DGV)控件,以便显示对象属性的值。修改对象的属性时,我无法自动更改DGV。

但是,如果我调用DGV.Refresh()方法,一切正常!

有没有一种方法可以在数据源发生变化时自动刷新DGV(无需手动调用Refresh()方法)?

这是我的代码:

Public Class Form1
Private objetos As New System.ComponentModel.BindingList(Of ClasePrueba)
Private bs As New BindingSource

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim a As New ClasePrueba(1.1, True, 1)
    Dim b As New ClasePrueba(2.2, False, 2)
    Dim c As New ClasePrueba(3.3, True, 3)
    objetos.Add(a)
    objetos.Add(b)
    objetos.Add(c)

    bs.DataSource = objetos
    dgvDatos.DataSource = bs

End Sub

Private Sub btnCambiarDatos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCambiarDatos.Click
    For Each objeto As ClasePrueba In bs
        objeto.Prop1 = objeto.Prop1 + 1.5
        objeto.Prop2 = Not objeto.Prop2
        objeto.Prop3 = objeto.Prop3 + 1
    Next
    'dgvDatos.Refresh()
End Sub
EndClass

谢谢!

0 个答案:

没有答案