为什么我不能将自定义对象列表绑定到datagridview? [英] Why can't I bind a list of custom objects to datagridview?
问题描述
在过去的两个小时中,我已经遍历您的问题以及互联网上的其他地方,无法在任何地方找到解决我问题的方法,或者至少我不理解。如果出现多余或无礼,我谨向您致歉。 让我清楚一点:问题是我以某种方式无法正确实施该方法,但我理解(或认为我确实)应该怎么做。
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.
我在表单上有一个网格视图,我想在其中显示代表约会的自定义对象。我想绑定到我的约会对象而不是数据表(成功)。但是,以下方法虽然看起来正确,但不会在网格中显示我的约会对象。此外,将对象直接添加到绑定源的内部列表中也无法在网格中显示它们,就像直接将gridview的数据源设置为绑定列表一样。我不知道我在做什么错!请帮忙,这似乎毫无意义,并且让我发疯。
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.
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)通过枚举器返回适配器的项目(约会)集合。我已经对此进行了测试,并且知道枚举数正在返回项目,所以问题就出在这里。
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.
推荐答案
您无法绑定到对象的公共字段。正如Microsoft所说:您可以绑定到任何公共语言运行库(CLR)对象的公共属性,子属性以及索引器。 MSdn绑定源概述。
将您的 ClsAppointment
类更改为此:
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 :
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
这篇关于为什么我不能将自定义对象列表绑定到datagridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!