过去两个小时我一直在浏览你的问题以及互联网上的其他地方,并且无法在任何地方找到我的问题的解决方案,或者至少我不理解它,如果我这样做的话。如果这看起来多余或无意,我会提前道歉。 让我明确一点:问题在于我在某种程度上没有正确实施这种方法,但我理解(或者认为我这样做)应该如何完成。
我在表单上有一个gridview,我想在其中显示代表约会的自定义对象。我想绑定到我的约会对象而不是数据表(这是成功的)。但是,下面的方法不会在网格中显示我的约会对象,尽管它看起来是正确的。此外,将对象直接添加到bindingsource的内部列表也无法在网格中显示它们,就像将gridview的数据源直接设置到bindinglist一样。我不知道我做错了什么!请帮助,这似乎毫无意义,让我发疯。
Public Sub DisplayItems()
Dim bindingsource As BindingSource
Dim appointment As ClsAppointment
Dim appointments As System.ComponentModel.BindingList(Of ClsAppointment)
Dim iterator As IEnumerator
appointments = New System.ComponentModel.BindingList(Of ClsAppointment)
bindingsource = New BindingSource
iterator = Items
While iterator.MoveNext '
appointment = iterator.Current
appointments.Add(appointment)
End While
bindingsource.DataSource = appointments
gridview.DataSource = bindingsource
Debug.Print("")
Debug.Print("DisplayItems()...")
Debug.Print("GridView has " & gridview.Rows.Count & " rows")
End Sub
Public Class ClsAppointment
Public FirstName As String
Public LastName As String
Public Day As String
Public [Date] As Date
Public Time As Date
Public Address As String
Public City As String
Public State As String
Public Zip As String
Public Description As String
End Class
=============================================== =========================================
注意:DisplayItems()是一个适配器(ItemEditor)的方法,为了简单起见,我选择不显示它。另一个方法(Items)通过枚举器返回适配器的项目集(约会)。我已经测试了这个,并且知道枚举器正在返回项目,所以问题不在于此。
答案 0 :(得分:3)
您无法绑定到对象的公共字段。正如微软所说:“你可以绑定任何公共语言运行时(CLR)对象的公共属性,子属性以及索引器。” Msdn- Binding Sources Overview。
将您的ClsAppointment
课程更改为:
Public Class ClsAppointment
Property FirstName As String
Property LastName As String
Property Day As String
Property [Date] As Date
Property Time As Date
Property Address As String
Property City As String
Property State As String
Property Zip As String
Property Description As String
End Class
答案 1 :(得分:0)
请允许我简化您的代码:
Public Sub DisplayItems()
gridview.DataSource = Me.Items()
Debug.Print("")
Debug.Print("DisplayItems()...")
Debug.Print("GridView has " & gridview.Rows.Count & " rows")
End Sub
试试这个,让我们知道你得到了什么错误。我知道你最终可能需要BindingSource,但是暂时让我们把它从图片中删除,看看它是如何工作的。