2013-05-18 48 views
0

我有一个对象的ArrayList(例如Employees)。如何在Datagridview中显示第二个类的成员

Employee类属性:

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

Employee类具有一个属性Workgroup,它包含一个Workgroup对象:

工作组类属性:

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

我试图显示所有的值,并设置列的DataPropertyName这些值:

  • 电子邮件
  • 电话“,
  • workgroup.name“,
  • workgroup.email“。

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

有没有一种简单的方法,而无需编写一个包装类,它暴露了员工和工作组的所有属性?

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

+0

你能比“这不行”更具体一点。例如,在调试器的输出窗口中是否有任何错误消息? – ChrisF

+0

“这不起作用”表示指定的列保持空白。这没有引起任何错误。 –

+0

很高兴你找到了解决方案。但是请你把它作为答案发布。然后你可以接受它来证明问题已经解决。 – ChrisF

回答

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 
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 

这项工作适合你吗?创建属性来显示工作组属性

+0

这将适用于这个例子,但我正在寻找一个通用的解决方案。我在我的项目中有大约90个不同的对象,与上面示例中的另一个对象相关。我更喜欢使用扩展DataGridViewColumn的解决方案,它可以处理这种DataProperties。 –

相关问题