簡體   English   中英

如何在Datagridview中顯示第二個類的成員

[英]How to display the members of a second Class in a Datagridview

我有一個對象的ArrayList(例如,Employees)。

員工類別屬性:

  • 名稱(字符串),
  • 電子郵件(字符串),
  • 電話(字符串),
  • 工作組(工作組)

Employee類具有一個屬性Workgroup,其中包含一個Workgroup對象:

工作組類屬性:

  • 名稱(字符串),
  • 電子郵件(字符串))。

我試圖顯示所有值並將列的DataPropertyName設置為以下值:

  • 名稱 ”,
  • 電子郵件 ”,
  • 電話 ”,
  • workgroup.name ”,
  • workgroup.email ”。

但這不適用於工作組屬性。

有沒有一種簡單的方法,而無需編寫一個包裝類,該包裝類公開了employee和workgroup的所有屬性?

我的項目中有許多具有相似關系的對象,並希望從具有本機sql的數據表遷移到像nhibenate這樣的對象關系映射器。 因此,為所有視圖編寫其他映射器類將非常昂貴。 我也在用Eclipse在Java中編程,在那里我可以使用Interface ITableLabelProvider解決此問題。

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

這對您有用嗎? 創建屬性以顯示工作組屬性

我找到了解決方案:

我添加了一個經過修改的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM