我正在嘗試將 BindingList 資料放入默認的 DataGridView,但沒有成功。如果我運行表單,它會顯示一個帶有空行的 DataGridView。所以行數是正確的,但是資料本身是缺失的。
Imports System.ComponentModel
Public Class Form1
Public Sub New()
InitializeComponent()
'Declare BindingList
Dim bList As BindingList(Of User)
'Init BindingList Constructor
bList = New BindingList(Of User)()
'List -> BindingList
'Dim bindingList = New BindingList(Of Person)(list)
'BindingList Settings
bList.AllowNew = True
bList.AllowRemove = True
bList.AllowEdit = True
bList.RaiseListChangedEvents = True
'Add Object
bList.Add(New User() With {.id = 1, .firstName = "Jeff", .lastName = "Tree"})
bList.Add(New User() With {.id = 2, .firstName = "Loes", .lastName = "Stone"})
'BindingList -> DataGridView
Dim source = New BindingSource(bList, Nothing)
DataGridView1.DataSource = bList
End Sub
End Class
Public Class User
Sub New()
End Sub
Public id As Integer
Public firstName As String
Public lastName As String
End Class
結果:
uj5u.com熱心網友回復:
該類User
僅包含公共欄位(不能用于資料系結)。您應該改用屬性:
Public Class User
Public Property Id As Integer
Public Property FirstName As String
Public Property LastName As String
End Class
然后,您可以洗掉手動添加到 DataGridView 的列(在這種情況下,它們將自動為您生成),或者您可以將AutoGenerateColumns
DGV 的屬性設定為 false,但在這種情況下,您還需要設定DataPropertyName
每列的屬性以匹配您的類中的屬性名稱。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/489799.html
下一篇:如何在c#中進行友好的cpu回圈