How to fix datagridview designer not returning idesignerhost service in user control

iLens 6 Reputation points

I have a user control which contains a DataGridView. I add a property "Columns" to the designer with

  <Editor(GetType(MyColumnCollectionEditor), GetType(UITypeEditor))>
    <Description("Add or Edit columns")>
    Public ReadOnly Property Columns As DataGridViewColumnCollection
            Return DGV.Columns
        End Get
    End Property

When I click the "Columns" property in the designer it invokes my "EditValue", but the line

     CType(provider.GetService(GetType(IDesignerHost)), IDesignerHost)

returns "Nothing"

The code is ...

Public Class MyColumnCollectionEditor
    Inherits UITypeEditor
    Private dataGridViewColumnCollectionDialog As Form
    Sub New()
    End Sub
    Public Overrides Function EditValue(ByVal context As ITypeDescriptorContext, ByVal provider As IServiceProvider, ByVal value As Object) As Object
        If provider IsNot Nothing AndAlso context IsNot Nothing Then
            Dim service = CType(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
            If service Is Nothing OrElse context.Instance Is Nothing Then Return value
            Dim host = CType(provider.GetService(GetType(IDesignerHost)), IDesignerHost)
            If host Is Nothing Then Return value
            If dataGridViewColumnCollectionDialog Is Nothing Then dataGridViewColumnCollectionDialog = CreateColumnCollectionDialog(provider)
            Dim dgv = CType(context.Instance, DataGridView)
            Dim oldSite = dgv.Site
            dgv.Site = CType(context.Instance, DataGridView).Parent?.Site
            SetLiveDataGridView(dataGridViewColumnCollectionDialog, dgv)
            Using transaction = host.CreateTransaction("DataGridViewColumnCollectionTransaction")
                If service.ShowDialog(dataGridViewColumnCollectionDialog) = DialogResult.OK Then
                End If
            End Using
            dgv.Site = oldSite
        End If
        Return value
    End Function

I also have code for

Private Shared Function CreateColumnCollectionDialog(ByVal provider As IServiceProvider) As Form


Public Shared Sub SetLiveDataGridView(ByVal form As Form, ByVal dgv As DataGridView)

But, of course, these never get called.

An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,662 questions
{count} votes