ICustomTypeDescriptor - list child properties can't be bound

0

I want to be able to add custom columns at runtime to show in a DataGridView, so I have implemented the ICustomTypeDescriptor interface. I then get an exception when trying to bind a child list within that object. My implementation of ICustomTypeDescriptor is below:

Public Function GetAttributes() As AttributeCollection Implements ICustomTypeDescriptor.GetAttributes
    Return TypeDescriptor.GetAttributes(Me)
End Function

Public Function GetClassName() As String Implements ICustomTypeDescriptor.GetClassName
    Return TypeDescriptor.GetClassName(Me)
End Function

Public Function GetComponentName() As String Implements ICustomTypeDescriptor.GetComponentName
    Return TypeDescriptor.GetComponentName(Me)
End Function

Public Function GetConverter() As TypeConverter Implements ICustomTypeDescriptor.GetConverter
    Return TypeDescriptor.GetConverter(Me)
End Function

Public Function GetDefaultEvent() As EventDescriptor Implements ICustomTypeDescriptor.GetDefaultEvent
    Return TypeDescriptor.GetDefaultEvent(Me)
End Function

Public Function GetDefaultProperty() As PropertyDescriptor Implements ICustomTypeDescriptor.GetDefaultProperty
    Return TypeDescriptor.GetDefaultProperty(Me)
End Function

Public Function GetEditor(editorBaseType As Type) As Object Implements ICustomTypeDescriptor.GetEditor
    Return TypeDescriptor.GetEditor(Me, editorBaseType)
End Function

Public Function GetEvents() As EventDescriptorCollection Implements ICustomTypeDescriptor.GetEvents
    Return TypeDescriptor.GetEvents(Me)
End Function

Public Function GetEvents(attributes() As Attribute) As EventDescriptorCollection Implements ICustomTypeDescriptor.GetEvents
    Return TypeDescriptor.GetEvents(Me, attributes)
End Function

Public Function GetProperties() As PropertyDescriptorCollection Implements ICustomTypeDescriptor.GetProperties
    Return GetPropertyDescriptors()
End Function

Public Function GetProperties(attributes() As Attribute) As PropertyDescriptorCollection Implements ICustomTypeDescriptor.GetProperties
    Return GetPropertyDescriptors()
End Function

Public Function GetPropertyOwner(pd As PropertyDescriptor) As Object Implements ICustomTypeDescriptor.GetPropertyOwner
    Return Me
End Function

Protected Function GetPropertyDescriptors() As PropertyDescriptorCollection
    If PropertyDescriptorList Is Nothing Then
        Dim properties As New List(Of PropertyDescriptor)
        For Each desc As PropertyDescriptor In TypeDescriptor.GetProperties(Me.GetType())
            properties.Add(desc)
        Next

        For Each aProp In DynamicPropDict
            Dim Attributes As AttributeCollection = Nothing
            If DynamicAttrPropDict.ContainsKey(aProp.Key) Then
                Attributes = New AttributeCollection(DynamicAttrPropDict(aProp.Key).ToArray)
            Else
                Attributes = New AttributeCollection
            End If
            If aProp.Value IsNot Nothing Then
                properties.Add(New BODynamicPropertyDescriptor(Me, aProp.Key, aProp.Value.GetType(), Attributes))
            Else
                properties.Add(New BODynamicPropertyDescriptor(Me, aProp.Key, GetType(Object), Attributes))
            End If
        Next

        PropertyDescriptorList = New PropertyDescriptorCollection(properties.ToArray())
    End If

    Return PropertyDescriptorList
End Function

This is the exception that I am getting

I am getting hold of all of the existing properties using TypeDescriptor.GetProperties(Me.GetType()) and the child list property is being added to the list so I do not understand why the binding can no longer find it. Does anyone have any ideas what I could be doing wrong?

Update: Added exception details:

System.ArgumentException
HResult=0x80070057
Message=DataMember property 'AddressList' cannot be found on the DataSource.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.ListBindingHelper.GetList(Object dataSource, String dataMember)
at System.Windows.Forms.BindingSource.ResetList()
at System.Windows.Forms.BindingSource.DataSource_Initialized(Object sender, EventArgs e)
at System.Windows.Forms.BindingSource.OnInitialized()
at System.Windows.Forms.BindingSource.System.ComponentModel.ISupportInitialize.EndInit()
at KAI.OctopusPMI.Accounting.PLMaintSupplierEditView_BO.InitializeComponent()
in E:\DevelopmentVSTS\OctopusPMI\OctopusPMIV5\V5.0.99.9999\Source\Business Modules\Accounting\Accounting\Ledgers\Purchase\Supplier\PLMaintSupplierEditView_BO\PLMaintSupplierEditView_BO.Designer.vb:line 4430
at KAI.OctopusPMI.Accounting.PLMaintSupplierEditView_BO..ctor() in E:\DevelopmentVSTS\OctopusPMI\OctopusPMIV5\V5.0.99.9999\Source\Business Modules\Accounting\Accounting\Ledgers\Purchase\Supplier\PLMaintSupplierEditView_BO\PLMaintSupplierEditView_BO.vb:line 15
This exception was originally thrown at this call stack:
[External Code]
KAI.OctopusPMI.Accounting.PLMaintSupplierEditView_BO.InitializeComponent() in PLMaintSupplierEditView_BO.Designer.vb
KAI.OctopusPMI.Accounting.PLMaintSupplierEditView_BO.New() in PLMaintSupplierEditView_BO.vb
.net
winforms
asked on Stack Overflow Jul 13, 2020 by Chris Mooney • edited Jul 15, 2020 by Chris Mooney

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0