Custom Enum in strongly typed DataSet
Recently I found a question about using Enums in a DataSet on StackOverflow. I was a little dissapointed at the answers people provided that were either down right wrong, or overly complicated. Sometimes I think people are so hungry for reputation points they forget that they are supposed to be answering questions.
Anyway, one poster recommended using strongly typed DataSets, but then advocated partial classes, or modifying the generated code. While a strongly typed DataSet is the way to go, you don't have to do anything fancy to get enum support.
Lets say we have just created a DataSet, and dropped a single DataTable into it:
Normally AwesomeId would just map to some integer key in a lookup table, and for whatever reason you have an enum that maps directly to this table in code:
public enum AwesomeEnum
What you want to avoid is having to cast this back and forth from an Int to an Enum. The nice folks at Microsoft were kind enough to give us a type property for each column in a DataSet that can be changed. If you select the column you want to map, and then navigate to the properties tab in Visual Studio, you will see that the column type is easily changeable.
Now all you have to do is substitute the fully qualified Enum Type into that field and you are good to go.
Are we there yet? Sort of. You could officially use this right now if you want, but chances are somehwere down the road you will eventually run into this gem (after a rebuild) when you attempt to open the designer.
In case this happens, don't worry you haven't permanently broken the designer, but you will have to manually edit the .xsd file. Navigate to your DataSet .xsd file and right click on it. Choose to open it up with the text editor of your choice, but you are better off just using the default Visual Studio text editor.
Normally I don't advocate editing raw autogened XML, but this one is an easy fix. Find the line that maps your column name to a specific type. It should look something like this:
<xs:element name="AwesomeId" msdata:DataType="StackOverflowExamples.Library.DataAccess.EnumDataSet.AwesomeEnum, StackOverflowExamples.Library, Version=126.96.36.199, Culture=neutral, PublicKeyToken=null" msprop:Generator_UserColumnName="AwesomeId" msprop:Generator_ColumnPropNameInRow="AwesomeId" msprop:Generator_ColumnVarNameInTable="columnAwesomeId" msprop:Generator_ColumnPropNameInTable="AwesomeIdColumn" type="xs:anyType" minOccurs="0" />
Take a look at the msdata:DataType attribute. As you can clearly see, our fully qualified type is listed here, but there is some assembly information that is causing us problems. Simply remove everything after the Type Name.
<xs:element name="AwesomeId" msdata:DataType="StackOverflowExamples.Library.DataAccess.EnumDataSet.AwesomeEnum" msprop:Generator_UserColumnName="AwesomeId" msprop:Generator_ColumnPropNameInRow="AwesomeId" msprop:Generator_ColumnVarNameInTable="columnAwesomeId" msprop:Generator_ColumnPropNameInTable="AwesomeIdColumn" type="xs:anyType" minOccurs="0" />
Now you can save the .xsd file, close it, and reopen your DataSet in the designer without any more problems.
Enjoy your strongly typed DataSet with custom Enum values :)