如何设置和检索组合框的值?

时间:2013-06-19 04:17:23

标签: c# combobox windows-mobile-6.5

我在Windows移动应用程序中有一个组合框。我已通过以下方式添加项目:


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

我已经完成了这个b'coz我想为combobx设置一个值字段,以便稍后在我的代码中使用。

在索引更改功能中,我想将所选值设置为变量。


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

但不知何故,即使我从组合框中选择值为2的第二项,这段代码也会返回0作为选定值。

还有其他方法吗?

2 个答案:

答案 0 :(得分:0)

在我的同事的帮助下,我得到了解决方案。

由于我使用ListItem将值和项添加到ComboBox,因此cmb_task.SelectedValue不起作用。

必须按以下方式对其进行类型转换以检索值。


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

答案 1 :(得分:-1)

如何在SelectedIndexChanged中选择ValueMember的值?

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