build propertygrid at runtime...

lidds

Well-known member
Joined
Oct 19, 2004
Messages
122
Programming Experience
Beginner
What I want to be able to do is add properties to my propertygrid at runtime depending on what properties the user has specified with a database table. Let me give you an example and hopefully it might all become clearer.

I have a propertygrid called 'propGrid1' which is placed on my form already at design time. I then have a database table which has 3 columns and info as shown below:

propertyName | propertyCategory | propertyType |
-----------------------------------------------------
textProp1 | textCate | text |
boolProp1 | booleanCate | boolean |

What I want to be able to do is read the contents of the table and in this case create 2 properties within my propertygrid (1) name = 'textProp1' which is in a propertygrid category called 'textCate' which allows text input (2) name='boolProp1' which is in a propertygrid category called 'booleanCate' and allows a 'true' or 'false' combobox.

I know how to hard code these properties in e.g.

<CategoryAttribute("textCate"), _
DescriptionAttribute("Hard coded text property")> _
Public Property textProp1() As String
Get
Return _textValue
End Get
Set(ByVal Value As String)
_textValue = Value
End Set
End Property

But I don't know how to create a property dynamically using information from a table. Does anyone have any idea on how I could do this, I am relatively new to vb.net so any type of example would be greatfull.

Thanks inadvance

Simon
 
First, create a class to hold the properties and their values:
VB.NET:
<DefaultProperty("textProp1")> _
Public Class MyProperties

    Private _textProp1 As String
    Private _boolProp1 As Boolean

    <Category("textCate"), _
     DefaultValue("Text1"), _
     Description("Enter some text")> _
    Public Property textProp1() As String
        Get
            Return _textProp1
        End Get
        Set(ByVal Value As String)
            _textProp1 = Value
        End Set
    End Property

    <Category("booleanCate"), _
     DefaultValue(True), _
     Description("Select True or False")> _
     Public Property boolProp1() As Boolean
        Get
            Return _boolProp1
        End Get
        Set(ByVal Value As Boolean)
            _boolProp1 = Value
        End Set
    End Property
End Class
Now in the form that contains the propertyGrid, create an instance of the class above, set the values for the class properties, then set the SelectedObject property of the PropertyGrid to the class instance:
VB.NET:
Private MyProps As New MyProperties()

Private Sub CreateProperties()
    MyProps.textProp1 = "Value of TextProp1"
    MyProps.boolProp1 = False
    PropertyGrid1.SelectedObject = MyProps
End Sub
You will want to set the values of the MyProperties instance to the values in the dataTable depending on the way you will be obtaining the data (either setup a dataReader or use a dataTable/dataAdapter).
 
Paszt,

The problem I have is that 1) I don't know how many properties are actually needed in the property grid untill I query my database e.g. could be:
1 datatimepicker
2 text
1 boolean
1 combobox

and then it could change the following day to:

2 datatimepicker
1 text
3 boolean
5 combobox

2) I don't know the name of the properties untill I query the database.
3) I don't know the name of the categories untill I query the database.

So what I want to be able to do is build the propertygrid dynamically at runtime depending on values in my database table, I think your code will only creates two properties???

Thanks

Simon
 
Back
Top