Question

I have a combo-box in a windows mobile application. I have added items to it in the following way:

        cmb_task.Items.Add(new ListItem(taskid.ToString(), taskname));

I have done this b'coz i want to set a value field to the combobx to use later in my code.

In index change function i want to set selected value to a variable.

     private void cmb_task_SelectedIndexChanged(object sender, EventArgs e)
     {
        taskid = Convert.ToInt32(cmb_task.SelectedValue);
     }

but somehow this piece of code is returning 0 as a selected value even if i select 2nd item from combobox with value=2.

Is there any other way to go about it?

Was it helpful?

Solution

I got solution for it, with help of one my colleague.

Since i had used ListItem to add value and item to ComboBox, cmb_task.SelectedValue didn't work.

It has to be type-casted in the following way to retrieve value.

    ListItem list = (ListItem)cmb_task.SelectedItem;
    taskid = Convert.ToInt32(list.ID);

OTHER TIPS

How to select the Value of ValueMember in SelectedIndexChanged?

Dim DTDep As DataView
Private Sub CargarUbicacion()
        Dim adapter As New SqlCeDataAdapter
        Dim comando As SqlCeCommandBuilder
        Dim Datos As New DataSet
        Dim Str As String
        Dim Consult As String


        Try
            Str = "select idUbicacion,Descripcion from Ubicacion order by Descripcion"

            Dim Cn As SqlCeConnection = GetConnection()
            adapter = New SqlCeDataAdapter(Str, Cn)
            adapter.Fill(Dset, "UBICACION")
            DTDep = Dset.Tables("UBICACION").DefaultView
            Me.cmbUbicaciones.DataSource = DTDep
            Me.cmbUbicaciones.DisplayMember = "Descripcion"
        Catch ex As Exception
            MsgBox("Error al cargar ubicaciones" & ex.Message)
        End Try
    End Sub
this ?
  Private Sub cmbUbicacion_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbUbicacion.SelectedValueChanged


        Me.lblIiUbiFin.Text = Convert.ToInt32(Me.cmbUbicacion.SelectedValue).ToString

    End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top