列表框不会显示值 [英] Listbox won't display value

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

问题描述

从用户控件中,我试图在名为PersonList的列表框中显示值

From my usercontrol I tried to show value inside a listbox named PersonList

用户控制代码:

BindingList<NotifiablePerson> PerSonList = new BindingList<NotifiablePerson>();

SqlCommand prsonListCmd = new SqlCommand("SQL QUERY", conn);
SqlDataReader dr = prsonListCmd.ExecuteReader();
 if (dr.HasRows)
   {
      while (dr.Read())
        {
           //collect value from database and save inside Fname and Lname variable

           NotifiablePerson np = PerSonList.AddNew();
           np.FirstName = Fname;
           np.LastName = Lname; 
        }               
  }
PersonList.DisplayMember = "np.FirstName" + "np.LastName";
PersonList.ValueMember = "np.FirstName";
PersonList.DataSource = PerSonList;

NotifiablePerson类代码:

namespace SMS
{
 class NotifiablePerson : MyComponentModel.NotifyProperyChangedBase
  {
    private string _firstName;
    public string FirstName
        {
         get { return _firstName; }
         set{
             if (this.CheckPropertyChanged<string>("FirstName", ref _firstName, ref value))
             {
               this.DisplayNameChanged();
             }
            }
        }

    private string _lastName;
    public string LastName
        {
        get { return _lastName; }
        set{
           if (this.CheckPropertyChanged<string>("LastName", ref _lastName, ref value))
             {
              this.DisplayNameChanged();
             }
           }
        }

        public string DisplayName
        {
            get { return _firstName + " " + _lastName; }
        }

        private void DisplayNameChanged()
        {
            this.FirePropertyChanged("DisplayName");
        }
    }
}

但是列表框仅显示 SMS.NotifiablePerson 的列表,而不显示我指定的实际值.在这里设置valuemember.而且它可以正常工作,并在边箱中显示相关的值.但是只有列表框不能显示正确的值.

But listbox only display a list of SMS.NotifiablePerson , not the actual value I specified. Here I set valuemember. And it works correctly and display related values in sidebox. But only listbox doesn't display correct value.

此代码有什么问题?

推荐答案

尝试更改此内容:

PersonList.DisplayMember = "np.FirstName" + "np.LastName";

对此:

PersonList.DisplayMember = "DisplayName";

这篇关于列表框不会显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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