vb.net 如何将 DataGridView 绑定到自定义类列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18056293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to bind a DataGridView to a list of custom classes
提问by bendataclear
I have a list of custom classes that I am building using a TableAdapter
.
我有一个使用TableAdapter
.
I want to add these to a DataGridView
binding certain columns only.
我只想将这些添加到DataGridView
绑定某些列中。
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.
我已经解决了一些关于 SO 的问题和一些教程,但它们似乎都与我需要的略有不同。
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.
有没有办法可以DataGridView
使用我的项目列表填写?DataSet
如果可以的话,我宁愿避免使用,因为我已经在这种List<Of Class>
类型上构建了很多其他代码。
Edit - Here is the Class Booking
declaration:
编辑 - 这是类Booking
声明:
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
回答by Toni Moreno
I know is really late for you but maybe it can help someone.
我知道对你来说真的很晚,但也许它可以帮助某人。
I had the same problem that you but using vb.net.
我和你有同样的问题,但使用 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.
尝试完成您的类添加{get; set;}
(如果使用 vb.net,则添加属性属性),一切都会正常。
Hope it can be helpful.
希望它可以有所帮助。
回答by varocarbas
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:
我不确定您是否获得了所需的IAllBookings
所有信息。在任何情况下,您都没有正确地将它传递给dgvBookings
. 这里有几个小代码可以帮助您了解如何更好地工作:
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.
该newTable
模拟完美的DataGridView
结构(列&行)并且因此可以作为一个DataSource
直接。
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:
您提供的信息少于预期(1D 与预期的 2D),因此结果不是您想要的。您需要提供更多信息;例如:您可以一一添加行,而不是依赖 DataSource:
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.
我希望这个答案能帮助您更好地理解如何处理DataGridView
和处理不同的数据源。
-- 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