StrataFrame Forum

Can I populate a DevExpress LookupEdit using an enum?

http://forum.strataframe.net/Topic7299.aspx

By Peter Jones - 3/4/2007

Hi,

I would like to use enums to populate a LookupEdit column in a DevExpress grid. I've worked my way to the BuildDataTableFromEnum method and that seemed to hold the clue but I can't work out how to use it. Before spending a lot of time on this I thought I had better ask if it is even possible. If it is, do you have a code snippet I could use?

Cheers, Peter

By Trent L. Taylor - 3/4/2007

This will require the use of an embedded control which we do not have a wrapper for.  However, you can use the BuildDataTableFromEnum static (shared) method in the MicroFour.StrataFrame.Tools.Common namespace to create the data table that can then be used as a data source within the LookupEdit control.  For us to provide the native SF Enum population within the embedded control would require use to re-write some of the DevExpress type editors which just is not feasible.  The good news is that you can at least get the final result you are looking for. Smile
By Peter Jones - 3/12/2007

Hi,

Finally got around to doing this. I can create a data table ok and it has two columns headed Description and Value. The problem is that each column contains the enumation description (Dev0, Dev1 etc) but no enumeration value (0,1,3 etc).

My code the create the tables is:

Dim dt As New Data.DataTable

dt = MicroFour.StrataFrame.Tools.Common.BuildDataTableFromEnum(GetType(TMS_Test_01.TMSEnums.DeviceType))

 

My code to create the enumeration is:

Public Enum DeviceType As Integer

Dev0 = 0

Dev1 = 1

Dev2 = 2

Dev3 = 3

End Enum

What elso do I need to do?

Cheers, Peter

 

By StrataFrame Team - 3/13/2007

When you create a DataTable from that tools method, the Value column of the DataTable will contain the actual enumeration value, not the integer tied to the enum value.
By Peter Jones - 3/13/2007

Hi,

Ok then. When I first looked at this issue that was the only method I could identify that could create an 'enum table'. Does SF have another method that will return the enum integer in the value column?

Cheers, Peter

By StrataFrame Team - 3/13/2007

I don't think we have one that will just get the integer value for the enum.  We originally had all of the method return a DataTable with the value column populated with an integer, but we discovered that you couldn't bind to a strong-typed property that had been typed as an enum, but you can bind an enum populated combo box to an integer-typed property, so we changed the methods to populate the value column with the actual enum value.  This all happened in version 1.1 or 1.2, I'm not sure. 

If you want to be able to populate a column with the integer value, you can use the CreateDataTableFromEnum method and add an extra column, say "ValueInt", to the table, cycle through the rows, and set the integer value.  Then you'll just have to change the ValueMember of the control to "ValueInt", not "value".

By Peter Jones - 3/13/2007

Hi,


For anyone who is interested the following seems to work ok when binding a DevExpress lookup edit to an internal enum. It involves creating a BO for each enum which, of course, gives great portability.


1. Make a generic view to use when creating an enum BO:

SELECT '' AS Description, '' AS Value


2. Created BO using the above view. In this example we are creating a BO for our DeviceType enums.


3. Created Business Binding Source mapped to the BO.


4. Add this code to the enum's BO's ParentFormLoading event:

   Private Sub boEnumDeviceType_ParentFormLoading() Handles Me.ParentFormLoading
        Dim dt As New Data.DataTable
        dt = MicroFour.StrataFrame.Tools.Common.BuildDataTableFromEnum(GetType(TMS_Test_01.TMSEnums.DeviceType))
        Me.CopyDataFrom(dt, MicroFour.StrataFrame.Business.BusinessCloneDataType.ClearAndFillFromCompleteTable)
        dt = Nothing
    End Sub

5. Created a LookUpEdit control in DevExpress using the BBS as the datasource and mapped Description as the display member and Value as the value member.


And that was it. The combo box shows the enum descriptions and the value saved to the database is enum's integer value.

So, all we need to do is create BO's for each of our enums that are visible on the UI and use them just as we would a table.

Cheers, Peter