How to bind a DataGridView to a list of custom classes

拥有回忆 提交于 2019-12-11 09:38:12

问题


I have a list of custom classes that I am building using a TableAdapter.

I want to add these to a DataGridView binding certain columns only.

I have tried the code below to fill and bind the data:

lAllBookings = (From r As DataRow In BookingsTableAdapter1.GetDataWithItems().Rows
                        Select New Booking With {.bookingID = r.Item("BookingID"), _
                                                 .itemID = r.Item("ItemID"), _
                                                 .bookedOutDate = r.Field(Of DateTime?)("BookedOutDate"), _
                                                 .bookedInDate = r.Field(Of DateTime?)("BookedInDate"), _
                                                 .identType = r.Item("IdentType"), _
                                                 .identString = r.Item("IdentString"), _
                                                 .image = r.Item("Image"), _
                                                 .complete = r.Item("Complete"), _
                                                 .notes = r.Item("Notes"), _
                                                 .itemName = r.Item("ItemName"), _
                                                 .itemBC = r.Item("ItemBarcode")}).ToList



        dgvBookings.Columns("BookingID").DataPropertyName = "bookingID"
        dgvBookings.Columns("ItemIdent").DataPropertyName = "itemName"
        dgvBookings.Columns("BookedOut").DataPropertyName = "bookedOutDate"
        dgvBookings.Columns("IdentString").DataPropertyName = "identString"


        dgvBookings.DataSource = lAllBookings

Now when I do this I get the correct number of rows but all fields are blank.

I've run through a few questions on SO and a few tutorials but they all seem to do things slightly different to what I need.

Is there a way I can fill the DataGridView using my list of items? I'd rather avoid using a DataSet if I can as I've built a lot of other code on this List<Of Class> type.

Edit - Here is the Class Booking declaration:

Public Class Booking

    Public bookingID As Integer
    Public itemID As Integer
    Public itemName As String
    Public itemBC As String

    Public identType As Short
    Public identString As String
    Public image As Byte()
    Public complete As Boolean
    Public notes As String

    Public bookedInDate As DateTime?
    Public bookedOutDate As DateTime?

End Class

回答1:


I know is really late for you but maybe it can help someone.

I had the same problem that you but using vb.net.

Finally I found the solution: Your class was not exposing properties but variables. The binding system looks for properties and can't find them so you get the empty rows.

Try to complete your class adding {get; set;} (or the Property attribute if using vb.net) and everything will work.

Hope it can be helpful.




回答2:


I am not sure that you are getting in IAllBookings all the information you want. In any case, you are not passing it rightly to the dgvBookings. Here you have a couple of small codes to help you to understand how this works better:

dgvBookings.Columns.Clear()    
Dim newTable As New DataTable

newTable.Columns.Add("Column1")
newTable.Columns.Add("Column2")
newTable.Columns.Add("Column3")

newTable.Rows.Add("1", "2", "3")
newTable.Rows.Add("1", "2", "3")
newTable.Rows.Add("1", "2", "3")

dgvBookings.DataSource = newTable

The newTable emulates perfectly the DataGridView structure (columns & rows) and thus it can be given as a DataSource directly.

Unlikely the case of a simple List:

dgvBookings.Columns.Clear()
Dim newList = New List(Of String)
newList.Add("1")
newList.Add("2")
newList.Add("3")

dgvBookings.DataSource = newList

You are providing less information than expected (1D vs. the expected 2D) and thus the result is not the one you want. You need to provide more information; for example: instead of relying on DataSource, you might add the rows one by one:

dgvBookings.Columns.Add("Column1", "Column1")
For Each item In newList
    dgvBookings.Rows.Add(item)
Next

I hope that this answer will help you to understand better how to deal with DataGridView and with the different data sources.

-- UPDATE

Row by row option applied to your specific case.

For Each item As Booking In lAllBookings
    With item
        dgvBookings.Rows.Add(.bookingID.ToString(), .itemID.ToString(), .bookedOutDate.ToString(), .identString.ToString())
    End With
Next


来源:https://stackoverflow.com/questions/18056293/how-to-bind-a-datagridview-to-a-list-of-custom-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!