我找到了一个解决方案:
我添加了一个改编的 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