Monthly Archives: October 2007

links for 2007-10-31

Databind a ComboBox to an Enum and your business object

Databinding intelligently in .net is usually pretty easy. Recently I wanted to quickly bind a combobox where the items are the values of an enum made up of single words and the selected value is tied to my data object. I was surprised that it wasn’t all that straighforward. Here’s me saving you some time in the future.

What you want is an easy way to get a list of objects that you can bind the combobox to. Here is a list you can easily create from any enum that will give you a handle on the names and values of the enum.

code:

public class ComboEnumList <V> : List < keyvaluepair < string,V> >
where V: struct //at least we can constrain that it ain't a class
{
public ComboEnumList():base()
{
//because generics don't allow a constraint like "where V: enum"
if (!typeof(V).IsEnum) throw new Exception( "New Argument Not An Enum " + (typeof(V).FullName));
Array values = Enum.GetValues(typeof(V));
foreach (object value in values)
{
this.Add(new KeyValuePair(Enum.GetName(typeof(V),value), (V) value));
}
}
}

To use this, you’d inherit an instance of this class with type V = your enum.
code:

public class AvailableJerseyStyles : ComboEnumList
{}
public enum JerseyStyleEnum{
Stripes,
Diamonds,
Paisley,
CheckerBoard,
Houndstooth
}

Now go into the VS2k5 designer, add a new project data source based on your AvailableJerseyStylesClass. On instantiation, create the AvailableJerseyStyles object and set it as the datasource for your bindingsource. DisplayMemeber = “Key”, ValueMember = “Value”.
Bind the enum property of your business object datasource to the SelectedValue of the AvailableJerseyStylesClass and you are good to go.

Hope it helps.

Offline and online

Google came out with a good idea a while ago called Google Gears.  The idea is to make some of these new fangled web applications able to function when you aren’t connected to the internet.

It never seemed like that great of an idea to me, but I’ve been using a personal Wiki to track household stuff with my pardner Sam.  I’ve been wishing for a wiki that I could use when I’m on the train and away from the internet.

I love the network.  When you are away from it though, why couldn’t you have a better cache – a mini net that is the last known version of what you seem to care about.  I’ve been using programs to download entire websites locally so I can read them while I commute.  It would be nice if you could just mark them as being of special interest in your browser.  Let computers hum and whir and keep it all up to date and in synch.  If we can do it with email, could we do it with the web, or at least the web I’m interested in?

Some folks think that offline and online will disappear as the network penetrates every corner of the world.  I doubt it.  Someone’s got to pay for it. More folks are interested in drinking water than BoingBoing, but it hasn’t penetrated every corner of the world.

Making the switch to Ubuntu

Yesterday morning, the 7.10 version of Ubuntu was released. It’s supposed to be chock full of goodness, so my neighbor Lawyer Matt and I had an install party with Aaron and Ian.

I dug out an old IBM Thinkpad T20 laptop that had been lying around and we got to it. I’d tried various Linux installs before, including Ubuntu. It always got down to having to know far to much about the internals than I wanted to.

This was a huge difference. The basic install went very smooth. I hit one snag. This old laptop had a PCMCIA wifi card that wasn’t recognized. Aaron found the solution for my Linksys WPC54G on the Ubuntu forums. With that out of the way, it is working. I’m impressed by how simple and smooth the experience is so far. Of course, the experience so far is mainly checking gmail, playing a movie, and writing this post.

links for 2007-10-18