Results 1 to 3 of 3

Thread: [RESOLVED] PropertyGrid and dynamic Enum from DB

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    2

    Resolved [RESOLVED] PropertyGrid and dynamic Enum from DB

    We are using the .NET PropertGrid to edit the Usersettings of our application. If we use Strings or integers than it works perfectly.

    Now we need a propery, we call them "Vendor", that store a GUID of the Vendor from a database. But the user didn´t see the GUID in the PG. he can choose beetwen the venders Names in a combobox. This combobox must be loaded from the database

    I have start to try to solve that with following code:
    Code:
    Public Overrides Function GetStandardValues(ByVal context As System.ComponentModel.ITypeDescriptorContext) As System.ComponentModel.TypeConverter.StandardValuesCollection
        Dim _Values As New Collections.SortedList
    
        If _dtVendors.Rows.Count > 0 Then
            For Each _rowVendors In _dtVendors.Rows
               _Values.Add(_rowVendors.VendorsGUID, _rowVendors.VendorsName)
            Next
        End If
    
        Return _Values
    
    End Function
    my question: What is the next step? how can i put the data from the SortedList to the needed standardValueCollection?`

    What are the next steps to reach the goal, that not the GUID was shown but the name of the vendor. And how can I store it?

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: PropertyGrid and dynamic Enum from DB

    So you have a property that returns a GUID, but you want the user to see the corresponding name instead of the GUID?

    And you are trying to solve this by using a custom TypeDescriptor? I suppose it might be possible (though I doubt it when the types aren't even the same), but why not simply make the GUID property non-browsable, and add a new property that converts to and from the GUID, but displays the name:
    Code:
    Private _Vendor As GUID
    
    <Browsable(False)> _
    Public Property Vendor() As GUID
       Get
          Return _Vendor
       End Get
       Set(ByVal value As GUID)
          _Vendor = value
       End Set
    End Property
    
    'Add new property that uses the GUID:
    Public Property VendorName() As String
       Get
          Dim guid As GUID = Me.Vendor
    
          ' use database to get Name from guid number
          Dim name As String = GetNameFromGuid(guid)
          Return name
       End Get
       Set(ByVal value As String)
          Dim guid As GUID = GetGuidFromName(value)
          Me.Vendor = guid
       End Set
    End Property
    The GetNameFromGuid method looks in the database and retrieves the name corresponding to the GUID passed, while the GetGuidFromName method does the reverse, it uses the name to look up the corresponding guid. This might be a problem if the name isn't unique, but you're going to have that problem anyway. You might also consider making the property ReadOnly, in which case there is no Setter, but the user cannot change the value (only read it).

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    2

    Re: PropertyGrid and dynamic Enum from DB

    Very nice and very easy

    It´s good that we spoke about that - Tank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width