组合框的SelectedValue [英] Combobox selectedvalue

查看:390
本文介绍了组合框的SelectedValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的形式(的WinForms)在组合框。在属性我已经设置的DisplayMember和ValueMember。的DisplayMember =名称和ValueMember = ID。组合框填充了以下对象:

I have a combobox on my form (winforms). In the properties I have set the DisplayMember and the ValueMember. DisplayMember = Name and ValueMember = ID. The Combobox is populated with the following objects:

public class MyObj
    {
        public string Name
        {
            get; set;
        }

        public int ID { get; set; }
    }



该名称将显示在下拉精(这样的DisplayMember工作),然而,当我这样做:

The Name displays fine in the dropdown(so DisplayMember is working) however, when I do:

mycombobox.SelectedValue它总是空。

mycombobox.SelectedValue it is ALWAYS null.

有谁知道,如果我忘了做什么?

Does anyone know if I've forgotten to do anything?

推荐答案

你有没有设置数据源属性。另外,还要确保你必须设置他们在正确的顺序 -

Have you set the DataSource property. Also make sure that you have to set them in the correct order -

将它们按以下顺序 -

Set them in the following order -

1. DisplayMember
2. ValueMember
3. DataSource

请参阅此链接 - <一个HREF =http://social.msdn.microsoft.com/Forums/en/winformsdatacontrols/thread/211a46f5-5971-4ea2-a61d-84e389360909相对=nofollow> http://social.msdn.microsoft.com /论坛/ EN / winformsdatacontrols /线程/ 211a46f5-5971-4ea2-a61d-84e389360909

See this link - http://social.msdn.microsoft.com/Forums/en/winformsdatacontrols/thread/211a46f5-5971-4ea2-a61d-84e389360909

另外,您可以使用的SelectedItem 属性来获取选定 MyObj中实例。

Alternatively you can use the SelectedItem property to get the selected MyObj instance.

这篇关于组合框的SelectedValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆