combo box valuemember

Joined
Sep 1, 2009
Messages
6
Programming Experience
5-10
VB.NET:
i am trying to get the valuemember of a combo box

the combo box is populated as follows

VB.NET:
Dim strSQL As String = "SELECT ageYear, ageBand from tblMobAgeBands WHERE firstGroup = 1 ORDER BY tableID"
        Dim objConnection As New SqlCeConnection(My.Resources.myConnection)
        Dim dsRenewalBands1 As New DataSet
        Dim objAdaptor As New SqlCeDataAdapter(strSQL, objConnection)
        Dim dtAgeBands1 As New DataTable
        Dim drDSRow As DataRow
        Dim drNewRow As DataRow

        Try
            objAdaptor.Fill(dsRenewalBands1, "dtAgeBands1")
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try


        dtAgeBands1.Columns.Add("id", GetType(System.Int32))
        dtAgeBands1.Columns.Add("band", GetType(System.String))

        'now populate with renewal bands
        For Each drDSRow In dsRenewalBands1.Tables("dtAgeBands1").Rows()
            drNewRow = dtAgeBands1.NewRow()
            drNewRow("id") = drDSRow("ageYear")
            drNewRow("band") = drDSRow("ageBand")
            dtAgeBands1.Rows.Add(drNewRow)
        Next

        With Me.cboAge1to20
            .DataSource = dtAgeBands1
            .DisplayMember = "band"
            .ValueMember = "id"
        End With

        Me.cboAge1to20.DropDownStyle = ComboBoxStyle.DropDownList

this populates the combo box fine

however when i try to access the valuemember like

VB.NET:
Me.txtAgeYear.Text = Me.cboAge1to20.ValueMember

the text box is populated with id rather than the value

can anyone help?
 
SelectedValue property expose the value of the selected items ValueMember.
 
hi, tried this but get the following error in vs2008 @ runtime

InvalidCastException was unhandled - conversion from type 'DataRowView' to type 'String' is not valid.
 
jaspalsinghkhokhar said:
InvalidCastException was unhandled - conversion from type 'DataRowView'
jaspalsinghkhokhar said:
.DataSource = dtAgeBands1
.DisplayMember = "band"
.ValueMember = "id"
Map the members before you set the datasource.
 
this worked fine, any explanation this occurs?
When the DataSource is set the selection change events (that you probably handle) may occur right away, when the ValueMember is not set the SelectValue is the selected bound item, ie the DataRowView which is the view of the DataRow.
 

Latest posts

Back
Top