Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

vb.net - 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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...