如何在Datagridview中显示第二个类的成员

时间:2013-05-18 18:22:26

标签: .net vb.net datagridview

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

员工类属性:

  • name(String),
  • 电子邮件(字符串),
  • 电话(字符串),
  • 工作组(工作组)

Employee类有一个属性Workgroup,它包含一个Workgroup-object:

工作组类属性:

  • name(String),
  • email(String))。

我尝试显示所有值,并将列的 DataPropertyName 设置为以下值:

  • 名称”,
  • 电子邮件”,
  • 电话”,
  • workgroup.name ”,
  • “的 workgroup.email ”。

但这对工作组属性不起作用。

是否有一种简单的方法可以不编写包装器类,从而公开员工和工作组的所有属性?

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

2 个答案:

答案 0 :(得分: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

这对你有用吗?创建属性以显示WorkGroup属性

答案 1 :(得分: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设置为CellTemplate到DatagridVieColumn:

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