问题
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.
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.
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/27771576/why-cant-i-bind-a-list-of-custom-objects-to-datagridview