StrataFrame Forum

How to populate comboboxes with options such as M/F for Male/Female (longer lists of course)

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

By AlexSosa - 10/3/2007

In .NET it is usual (recommended?) to use integers and ennumerations to identify options.  Nevertheless, I still like to use single letters such as M/F for Male/Female etc and I can get away with it since I don't deal in multilingual apps.

The question is, what is the best way to I populate comboxes using Strataframe in those cases?

TIA,

Alex

By Peter Jones - 10/3/2007

Hi ALex,

You can still use an enum but show a single letter (or a full name) to the user. Our enum BO's just have the following code:

    Private Sub boenBatchTypes_ParentFormLoading() Handles Me.ParentFormLoading
        Dim ColDesc As String = "Description"
        Dim dt As New Data.DataTable

        dt = MicroFour.StrataFrame.Tools.Common.BuildDataTableFromEnum(GetType(UtilEnums.clsEnum.BatchTypes))

        ' Localise Batch names
        For i As Integer = 0 To dt.Rows.Count - 1
            dt.Rows.Item(i).Item(ColDesc) = UtilLocalisation.clsLocalise.Localise(dt.Rows.Item(i).Item(ColDesc).ToString)
        Next

        dt.DefaultView.Sort = ColDesc
        Me.CopyDataFrom(dt.DefaultView.ToTable, MicroFour.StrataFrame.Business.BusinessCloneDataType.ClearAndFillFromCompleteTable)

        dt = Nothing
    End Sub

In this example:

.BuildDataTableFromEnum - standard SF function to extract our "batch name" enums and creates a data table

UtilLocalisation.. - is our own function to localise the enum description.

Me.CopyDataFrom - standard SF sub to load the populate the BO.

Easy as and works a treat.


Cheers, Peter

By Greg McGuffey - 10/3/2007

You might also use an attribute on each enum value to indicate an alternate value to use. See this post:



http://forum.strataframe.net/FindPost5781.aspx
By AlexSosa - 10/3/2007

Thank you Peter,

I am still a newbie so I can' t tell from reading your code if your technique stores a number or a character in the table?  I'd like to store a character in the table.

Similar problem with optionbuttons.  How do you handle that one?

Thanks,

Alex

By Peter Jones - 10/3/2007

Hi Alex,

We store the enum (integer) value so the above wouldn't work for you.

I've never had to do the same with an option group so I can't help with that one.

Cheers, Peter

By AlexSosa - 10/4/2007

Thank you Peter.

Alex

By Trent L. Taylor - 10/4/2007

Alex,

The question is, what is the best way to I populate comboxes using Strataframe in those cases?

You can use any data type you need as it relates to binding.  In this example you may not want to use an enum, as these will always be stored as an integer.  You will want to create your data source manually:

Dim myTable As New DataTable("")
Dim newRow As DataRow

'-- Create the columns
myTable.Columns.Add("display",GetType(String))
myTable.Columns.Add("value", GetType(String))

'-- Then populate your table
newRow = MyTable.NewRow()
newRow.Items("display") = "Apple"
newRow.Items("value") = "A"
MyTable.Rows.Add(newRow)

'-- Once populated, set the combo
MyCombo.DisplayMember = "display"
MyCombo.ValueMember = "value"
MyCombo.DataSource = MyTable

You will bind to the combo just as you would for an enum, the difference is that the SelectedValue or value member of the combo will bind to the [value] field in the table which you have defined as a string, which in return will allow you to bind to a character field, which ultimately gets saved back to the database as character.  Hope this helps. Smile