【问题标题】:Binding properties of a custom class to a WinForms DataGridView将自定义类的属性绑定到 WinForms DataGridView
【发布时间】: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


【解决方案1】:

事实证明,清理和重建解决方案解决了我的问题!将来会更频繁地采取这些步骤。

【讨论】:

    猜你喜欢
    • 2011-07-28
    • 1970-01-01
    • 2011-09-15
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 2014-05-12
    • 1970-01-01
    相关资源
    最近更新 更多