Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / Visual Basic
Article

Convert Enums to DataTables

Rate me:
Please Sign up or sign in to vote.
3.79/5 (10 votes)
24 Aug 2004 56.4K   24   5
An article on converting Enums to DataTables

Introduction

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 i.e.:

  • EnumToDataTable(GetType(EnumerationName)
            , "", "") 
VB.NET
' Test enumeration
Private Enum ETestEnum
    Enum_Item_A = 0
    Enum_Item_B = 1
    Enum_Item_C = 2
End Enum

'--------------------------------------------------------
' Use this little function to test it
'--------------------------------------------------------

Private Sub Test()
    Dim oData As DataTable

    ' Notice that we must use 'GetType(Enumeration)'
    oData = EnumToDataTable(GetType(ETestEnum), "KEY", "VALUE")
    
End Sub

Now here comes the real code

VB.NET
'-------------------------------------------
' Desription: Convert a given enum into a datatable
'
' Parameters: EnumObject - type of the enum
'             KeyField   - name for the key column, if not 
'                 supplied it will be set to "KEY" 
'             ValueField - name for the vaue column, if not 
'                 supplied it will be set to "VALUE" 
'--------------------------------------------

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

    '-------------------------------------------------------------
    ' Sanity check
    If KeyField.Trim() = String.Empty Then
        KeyField = "KEY"
    End If

    If ValueField.Trim() = String.Empty Then
        ValueField = "VALUE"
    End If
    '-------------------------------------------------------------

    '-------------------------------------------------------------
    ' Create the DataTable
    oData = New DataTable

    oColumn = New DataColumn(KeyField, GetType(System.Int32))
    oData.Columns.Add(KeyField)

    oColumn = New DataColumn(ValueField, GetType(System.String))
    oData.Columns.Add(ValueField)
    '-------------------------------------------------------------

    '-------------------------------------------------------------
    ' Add the enum items to the datatable
    For Each iEnumItem As Object In [Enum].GetValues(EnumObject)
        oRow = oData.NewRow()
        oRow(KeyField) = CType(iEnumItem, Int32)
        oRow(ValueField) = StrConv(Replace(iEnumItem.ToString(), "_", " "), _
              VbStrConv.ProperCase)
        oData.Rows.Add(oRow)
    Next
    '-------------------------------------------------------------

    Return oData

End Function

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThis will Create a List of Enum in a ListBox Pin
aaroncampf24-Nov-10 12:46
aaroncampf24-Nov-10 12:46 
GeneralC# Pin
RipplingCreek8-Jul-08 7:58
RipplingCreek8-Jul-08 7:58 
GeneralExcellent for populating options... Pin
karenpayne5-Sep-06 7:01
karenpayne5-Sep-06 7:01 
GeneralUses Pin
stixoffire11-Jul-06 4:16
stixoffire11-Jul-06 4:16 
GeneralI agree Pin
Ricardo Casquete17-Feb-06 23:36
Ricardo Casquete17-Feb-06 23:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.