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

iLens 6 Reputation points
2023-11-30T10:07:48.2066667+00:00

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

  <Editor(GetType(MyColumnCollectionEditor), GetType(UITypeEditor))>
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
    <Description("Add or Edit columns")>
    Public ReadOnly Property Columns As DataGridViewColumnCollection
        Get
            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
                    transaction.Commit()
                Else
                    transaction.Cancel()
                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

and

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

But, of course, these never get called.

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