views:

23

answers:

1

I'm having trouble on how to code this. First I need to assign a value for every item in the list (1 for the first item, 2 for the 2nd and so on). Does this control have a separate field for the value aside from text?

Second, I need to insert this into my database(MSSQL 2008) in this format:

insert into table values(ID,selectedvaluegoeshere)

Can someone guide me on how to achieve this.

+1  A: 

The ideal way to do this would be to use Enumerated Values to fill your Checked Combo Box, this allows you to guarantee the association of the items in the list with their values. To convert the Enum to a human friendly format you can do something like this.

Imports System.ComponentModel
Imports System.Reflection

Public Enum ePregnancyCode
            <Description("Not Specified")> _
            NotSpecified
            <Description("Not Pregnant")> _
            NotPregnant
            <Description("Pregnant")> _
            Pregnant
        End Enum

And then retrieve the Description field using this code

Public Function GetDescription(ByVal EnumConstant As [Enum]) As String
            Dim fi As FieldInfo = EnumConstant.GetType().GetField(EnumConstant.ToString())
            Dim aattr() As DescriptionAttribute = _
                DirectCast( _
                    fi.GetCustomAttributes(GetType(DescriptionAttribute), False),  _
                    DescriptionAttribute() _
                )
            Return If(aattr.Length > 0, aattr(0).Description, EnumConstant.ToString())
        End Function

You would probably be better off using a lookup edit for your Combo Selection now that I think about it. Then you can just retrieve the selected item collection from the Control when you go to insert into the database.

msarchet
would placing individual checkboxes inside a groupbox easier to insert?
Dumont
@Dumont, if you don't expect the options to change it would work well. You would have to check every check box though instead of just a collection.
msarchet