首页 > 解决方案 > 当值来自 WCF 服务时,不会设置 ComboBox SelectedValue

问题描述

我遇到了一个奇怪的问题,即未设置组合框的 SelectedValue 并且始终保持其默认的第一个元素。我正在使用带有 .Net 4.6.1 的 Windows 窗体

组合框数据源来自 WCF 服务(返回列表),我想知道这是否可能是问题所在。这是我到目前为止所做的,我标记了不起作用的部分。

奇怪的是我可以看到该值设置正确(使用调试器)但组合框仍然没有改变。

        private void ReporteTecnicoVerDetalle_Load(object sender, EventArgs e) {
            try {
                cmbTecnico.DataSource = util_service.ListarPersonas(5);
                cmbTecnico.DisplayMember = "Nombre_persona";
                cmbTecnico.ValueMember = "Id_persona";
            } catch (Exception ex) {
                MessageBox.Show("Error al poblar las personas: " + ex.Message);
            }

            try {
                cmbProyecto.DataSource = util_service.ListarProyectos();
                cmbProyecto.DisplayMember = "Nom_proyecto";
                cmbProyecto.ValueMember = "Id_proyecto";
            } catch (Exception ex) {
                MessageBox.Show("Error al poblar los proyectos: " + ex.Message);
            }

            ReporteTecnicoBE rtecBE = tecSvc.MostrarReporte(idReporte);
            
            // The options get populated properly above, but when I set the Selected value it does not work
            cmbProyecto.SelectedValue = rtecBE.Id_proyecto; // "Id_proyecto" is an int
            cmbTecnico.SelectedValue = rtecBE.Id_tecnico; // "Id_tecnico" as is this
          
            // All other fields work fine
            txtDetalle.Text = rtecBE.Detalles_reporte;
            txtId.Text = rtecBE.Id_proyecto.ToString();
            dtpFecha.Text = rtecBE.Fecha_reporte.ToString();
        }

谢谢!

标签: c#winformswcf

解决方案


推荐阅读