I have looked through your questions as well as elsewhere on the internet for the past two hours and cannot find a solution for my problem anywhere, or at least I didn't understand it if I did. I apologize in advance if this appears redundant or inane. Let me be clear: the issue is that I am somehow NOT implementing the approach correctly, but I understand (or think I do) how it is supposed to be done.
在过去的两个小时里,我在网上查过你的问题,也查过你的问题,但在任何地方都找不到解决问题的办法,或者至少我不理解。如果这显得多余或空洞,我提前道歉。让我澄清一下:问题是我在某种程度上没有正确地实现该方法,但我理解(或认为我理解)应该如何实现该方法。
I have a gridview on a form in which I want to display custom objects representing appointments. I want to bind to my appointment objects not a datatable (which was successful). However, the below approach will not display my appointment objects in the grid although it appears correct. Furthermore, adding objects directly to the bindingsource's internal list also fails to show them in the grid, as does setting the datasource of the gridview to the bindinglist directly. I have no idea what I am doing wrong! Please help, this seems to make no sense at all and is driving me crazy.
我在窗体上有一个gridview,我想在其中显示表示约会的自定义对象。我想绑定到我的约会对象,而不是datatable(它成功了)。然而,下面的方法将不会在网格中显示我的约会对象,尽管它看起来是正确的。此外,直接向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
========================================================================================
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Note: DisplayItems() is a method of an adapter (ItemEditor) which I chose not to show for simplicity's sake. Another method (Items) returns the adapter's collection of items (appointments) via an enumerator. I have tested this and know that the enumerator is returning the items so the problem is not this.
注意:DisplayItems()是一个适配器(ItemEditor)的方法,为了简单起见,我选择它不显示。另一个方法(项)通过枚举数返回适配器的项(约会)集合。我已经测试过这个,并且知道枚举器正在返回项,所以问题不是这个。
2 个解决方案
#1
2
You can not bind to public fields of an object. As Microsoft states "You can bind to public properties, sub-properties, as well as indexers, of any common language runtime (CLR) object." Msdn- Binding Sources Overview. Change your ClsAppointment
class to this :
不能绑定到对象的公共字段。正如Microsoft所说,“您可以绑定到任何公共语言运行时(CLR)对象的公共属性、子属性以及索引器。”Msdn——绑定源概述。将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
#2
0
Allow me to simplify your code:
请允许我简化您的代码:
Public Sub DisplayItems()
gridview.DataSource = Me.Items()
Debug.Print("")
Debug.Print("DisplayItems()...")
Debug.Print("GridView has " & gridview.Rows.Count & " rows")
End Sub
Try this, and let us know what errors you get. I know you might eventually need the BindingSource, but for the moment let's cut that out of the picture and see how things work.
试试这个,让我们知道你有什么错误。我知道你最终可能需要这个BindingSource,但是现在我们把它从图片中删掉,看看它是如何工作的。
#1
2
You can not bind to public fields of an object. As Microsoft states "You can bind to public properties, sub-properties, as well as indexers, of any common language runtime (CLR) object." Msdn- Binding Sources Overview. Change your ClsAppointment
class to this :
不能绑定到对象的公共字段。正如Microsoft所说,“您可以绑定到任何公共语言运行时(CLR)对象的公共属性、子属性以及索引器。”Msdn——绑定源概述。将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
#2
0
Allow me to simplify your code:
请允许我简化您的代码:
Public Sub DisplayItems()
gridview.DataSource = Me.Items()
Debug.Print("")
Debug.Print("DisplayItems()...")
Debug.Print("GridView has " & gridview.Rows.Count & " rows")
End Sub
Try this, and let us know what errors you get. I know you might eventually need the BindingSource, but for the moment let's cut that out of the picture and see how things work.
试试这个,让我们知道你有什么错误。我知道你最终可能需要这个BindingSource,但是现在我们把它从图片中删掉,看看它是如何工作的。