0

我有一个对象的 ArrayList(例如员工)。

员工类属性:

  • 名称(字符串),
  • 电子邮件(字符串),
  • 电话(字符串),
  • 工作组(工作组)

Employee 类有一个属性 Workgroup,其中包含一个 Workgroup 对象:

工作组类属性:

  • 名称(字符串),
  • 电子邮件(字符串))。

我尝试显示所有值并将列的DataPropertyName设置为这些值:

  • 名称”,
  • 电子邮件”,
  • 电话”,
  • 工作组名称”,
  • 工作组.电子邮件”。

但这不适用于工作组属性。

有没有一种简单的方法,无需编写包装类,即可公开员工和工作组的所有属性?

我的项目中有许多具有相似关系的对象,并且希望从具有本机 sql 的数据表迁移到像 nhibenate 这样的对象关系映射器。因此,为所有视图编写额外的映射器类会非常昂贵。我也在用 Eclipse 在 Java 中编程,我可以使用 ITableLabelProvider 接口解决这个问题。

4

2 回答 2

0
Friend Class Employee
    Public name As String
    Public Phone As String
    Public Email As String
    Private oWorkGroup As workGroup

    Public Property WorkGroupEmail As String
        Get
            Return oWorkGroup.Email
        End Get
        Set(value As String)
            oWorkGroup.Email = value
        End Set
    End Property
    Public Property WorkGroupName As String
        Get
            Return oWorkGroup.Name
        End Get
        Set(value As String)
            oWorkGroup.Name = value
        End Set
    End Property
End Class

这对你有用吗?创建属性以显示工作组属性

于 2013-05-18T18:58:12.193 回答
0

我找到了一个解决方案:

我添加了一个改编的 DataGridViewTextBoxCell:

Public Class SpecialDataGridViewTextBoxCell
    Inherits DataGridViewTextBoxCell

    Public Sub New()
        MyBase.new()
    End Sub

    'Overriding basic function
    Protected Overrides Function GetFormattedValue( _
            ByVal value As Object, _
            ByVal rowIndex As Integer, _
            ByRef cellStyle As DataGridViewCellStyle, _
            ByVal valueTypeConverter As System.ComponentModel.TypeConverter, _
            ByVal formattedValueTypeConverter As System.ComponentModel.TypeConverter, _
            ByVal context As DataGridViewDataErrorContexts _
        ) As Object
        Dim dataproperty As String = Me.OwningColumn.DataPropertyName
        If Not dataproperty Is Nothing AndAlso dataproperty.IndexOf(".") > 0 Then
            If value Is Nothing Then
                Return getValueByDottedProperty(Me.OwningRow.DataBoundItem, dataproperty)
            Else
                Return getValueByDottedProperty(value, dataproperty)
            End If
        Else
            Return MyBase.GetFormattedValue(value, rowIndex, cellStyle, valueTypeConverter, formattedValueTypeConverter, context)
        End If
    End Function

    Private Function getValueByDottedProperty(ByVal obj As Object, ByVal dataPropertyName As String) As String
        If Not obj Is Nothing AndAlso Not obj Is System.DBNull.Value Then
            If Not dataPropertyName Is Nothing AndAlso dataPropertyName.Length > 0 AndAlso dataPropertyName.IndexOf(".") > 0 Then
                Dim part1 As String = dataPropertyName.Substring(0, dataPropertyName.IndexOf("."))
                Dim part2 As String = dataPropertyName.Substring(dataPropertyName.IndexOf(".") + 1)
                Dim val As Object = getPropertyFromObject(obj, part1)
                If Not val Is Nothing AndAlso Not val Is System.DBNull.Value Then
                    Return getValueByDottedProperty(val, part2)
                Else
                    Return ""
                End If
            Else
                Dim val As Object = getPropertyFromObject(obj, dataPropertyName)
                If Not val Is Nothing AndAlso Not val Is System.DBNull.Value Then
                    Return val.ToString
                Else
                    Return ""
                End If
            End If
        Else
            Return ""
        End If
    End Function

    Private Function getPropertyFromObject(ByVal obj As Object, ByVal propertyName As String) As Object
        Dim pInfo As System.Reflection.PropertyInfo = obj.GetType.GetProperty(propertyName)
        If pInfo Is Nothing Then
            Return Nothing
        End If
        Dim val As Object = pInfo.GetValue(obj, Nothing)
        Return val
    End Function

End Class

我添加了一个函数来生成 DatagridViewTextBoxColumn:

Public Shared Function createSpecialDataGridViewTextBoxColumn(ByVal headername As String, ByVal datapropertyname As String) As DataGridViewTextBoxColumn
    Dim specialTextBoxColumn As New DataGridViewTextBoxColumn()
    With specialTextBoxColumn
        .DataPropertyName = datapropertyname
        .HeaderText = headername
        .CellTemplate = New SpecialDataGridViewTextBoxCell
    End With
    Return specialTextBoxColumn
End Function

然后我将适应的 DataGridViewTextBoxCell 设置为 DatagridVieColumn 的 CellTemplate:

With Me.DataGridView1
    .AutoGenerateColumns = False
    .Columns.Clear()
    .Columns.Add(createSpecialDataGridViewTextBoxColumn("Name", "name"))
    .Columns.Add(createSpecialDataGridViewTextBoxColumn("Email", "email"))
    .Columns.Add(createSpecialDataGridViewTextBoxColumn("Telephone", "telephone"))
    .Columns.Add(createSpecialDataGridViewTextBoxColumn("Name (Wrkgrp)", "workgroup.name"))
    .Columns.Add(createSpecialDataGridViewTextBoxColumn("Email (Wrkgrp.)", "workgroup.email"))
End With
于 2013-05-20T00:37:35.383 回答