The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
This is just a quick trick to convert an
Enum into a
DataTable, not sure if it would be of any practical use.
There are no projects to download since is just a
code snippet, copy and paste from this page and you are ready to go, well... let
me know what you think about it.
Using the code
Ok here it goes, as I mentioned just copy and paste, remember that to call the
function you will have to use the
GetType on the enumeration
, "", "")
Private Enum ETestEnum
Enum_Item_A = 0
Enum_Item_B = 1
Enum_Item_C = 2
Private Sub Test()
Dim oData As DataTable
oData = EnumToDataTable(GetType(ETestEnum), "KEY", "VALUE")
Now here comes the real code
Public Function EnumToDataTable(ByVal EnumObject As Type, _
ByVal KeyField As String, ByVal ValueField As String) As DataTable
Dim oData As DataTable = Nothing
Dim oRow As DataRow = Nothing
Dim oColumn As DataColumn = Nothing
If KeyField.Trim() = String.Empty Then
KeyField = "KEY"
If ValueField.Trim() = String.Empty Then
ValueField = "VALUE"
oData = New DataTable
oColumn = New DataColumn(KeyField, GetType(System.Int32))
oColumn = New DataColumn(ValueField, GetType(System.String))
For Each iEnumItem As Object In [Enum].GetValues(EnumObject)
oRow = oData.NewRow()
oRow(KeyField) = CType(iEnumItem, Int32)
oRow(ValueField) = StrConv(Replace(iEnumItem.ToString(), "_", " "), _