【发布时间】:2016-04-26 20:58:26
【问题描述】:
我编写了一个类,并希望它的属性显示在 DataGridView 控件中。但是,只有部分属性可用于绑定到列。如何使我的所有公共属性都可用于绑定到列?下面是我的课程,IndividualServices 属性未显示在设计器中,而其他属性则显示。
Public Class Patient
Private _name As String
Private _id As String
Private _services As New List(Of Service)
Property Name As String
Get
Return _name
End Get
Set(value As String)
_name = value
End Set
End Property
Property ID As String
Get
Return _id
End Get
Set(value As String)
_id = value
End Set
End Property
Public ReadOnly Property Services As List(Of Service)
Get
Return _services
End Get
End Property
ReadOnly Property TotalServices As Integer
Get
Dim i As Integer = 0
For Each s As Service In _services
i = i + s.Count
Next s
Return i
End Get
End Property
ReadOnly Property IndividualServices As Integer
Get
Return _services.Count
End Get
End Property
Public Sub Add(sName As String, sCode As String)
Dim bool As Boolean = False
If _services.Count = 0 Then GoTo firstThrough
For Each s As Service In _services
If s.Name = sName Then
s.Add(1)
bool = True
Exit For
End If
Next
If Not bool Then
firstThrough:
Dim newSer As New Service
newSer.Name = sName
newSer.Code = sCode
newSer.Count = 1
_services.Add(newSer)
End If
End Sub
End Class
【问题讨论】:
-
到目前为止你都尝试过什么?您是否尝试过返回“_services.Count”而不是“Services.Count”?
-
设计师没有变化,但感谢您指出这一点!进行了编辑。我将它添加为仅为 DataGridView 的属性,否则我一直在调用 Patient.Services.Count
-
离题但强烈建议:避免使用
GoTo声明。 -
您的代码工作正常,请确保您有一个有效的构建。还要确保您已将所有列添加到
DataGridView。例如,如果您最近在将网格绑定到该类型后添加了该列,则应手动将该列添加到您的网格中。 -
清理和重建我的解决方案解决了问题,感谢您的建议!将来会更频繁地采取这些步骤。
标签: vb.net winforms datagridview