Dot Net Tips and Tricks

Custom Search

CSharp IExtenderProvider with ExpandableObjectEditor for ValueLists

I’ve recently found a solution to a common problem of form development. Value lists or “CodeSets” for controls on user forms. Most of the time a programmer will write a stored procedure or statement to return some dataset and bind the results to a combo box, listview or similar control. I have several problems with this pattern.

  • Can cause multiple queries to the database for a single form to load.
  • Requires the programmer to write code for every control with an associated lookup table or value list.
  • Could be implemented inconsistently.

What are our goals?

  • Should require no code on the part of the programmer.
  • Should cache the values of the codesets at runtime to avoid multiple database queries.
  • Should be expandable to show both the name and id of the codeset.

So let’s solve this problem. I’m using Enterprise Library’s cache manager for caching and also EntLib’s data libraries for data access.

You’ll need 2 tables in the database:

  1. Codesets: The list of codeset identifiers and their descriptions.
  2. Codeset_Details: The list of values, it’s display value and the codeset identifier.

We create a custom component that implements IExtenderProvider that exposes a property called Codeset of type CodesetProperty.

Figure 1

The CodesetProperty class has a int and string member to store the id and name of the codeset to load into the control.

Figure 2

We need a custom typeeditor to show the available codeset values. NOTE: You will need to ensure you have access to the connection string for the current database. I do this through a registry setting pointing to our standard development directory which contains our app.config.

Figure 3

The last part is a custom typeeditor to return an InstanceDescriptor which the CodedDomSerializer will use to generate the Intialization code.

Figure 4

We then add a CodesetProvider to a form. In my case any combobox, listbox, dropdown and comboeditor will show a property “Codeset on codesetProvider1″. This is an expandable property so you’ll see the values listed but by dropping down the list on the property you will get a list of the available codesets and then at runtime this property will be used to load the combo box with the values.

No comments yet. Be the first.

Leave a reply