C# Listbox赢得';t显示值

C# Listbox赢得';t显示值,c#,winforms,listbox,inotifypropertychanged,C#,Winforms,Listbox,Inotifypropertychanged,从我的usercontrol中,我试图在名为PersonList的列表框中显示值 用户控制代码: BindingList<NotifiablePerson> PerSonList = new BindingList<NotifiablePerson>(); SqlCommand prsonListCmd = new SqlCommand("SQL QUERY", conn); SqlDataReader dr = prsonListCmd.ExecuteReader()

从我的usercontrol中,我试图在名为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;
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");
        }
    }
}
BindingList PerSonList=newbindingList();
SqlCommand prsonListCmd=新的SqlCommand(“SQL查询”,conn);
SqlDataReader dr=prsonListCmd.ExecuteReader();
如果(哈斯罗博士)
{
while(dr.Read())
{
//从数据库收集值并保存在Fname和Lname变量内
NotifiablePerson np=PerSonList.AddNew();
np.FirstName=Fname;
np.LastName=Lname;
}               
}
PersonList.DisplayMember=“np.FirstName”+“np.LastName”;
PersonList.ValueMember=“np.FirstName”;
PersonList.DataSource=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;
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:MyComponentModel.NotifyPropertyChangedBase
{
私有字符串_firstName;
公共字符串名
{
获取{return\u firstName;}
设置{
if(this.CheckPropertyChanged(“FirstName”,ref _FirstName,ref value))
{
this.DisplayNameChanged();
}
}
}
私有字符串_lastName;
公共字符串姓氏
{
获取{return\u lastName;}
设置{
if(this.CheckPropertyChanged(“LastName”,ref\u LastName,ref value))
{
this.DisplayNameChanged();
}
}
}
公共字符串显示名
{
获取{return\u firstName++\u lastName;}
}
私有void DisplayNameChanged()
{
此.FireProperty已更改(“显示名称”);
}
}
}
但是listbox只显示SMS.NotifiablePerson的列表,而不是我指定的实际值。我在这里设置
valuemember
。它工作正常,并在侧框中显示相关值。但只有listbox没有显示正确的值

此代码有什么问题?

请尝试更改此代码:

PersonList.DisplayMember = "np.FirstName" + "np.LastName";
为此:

PersonList.DisplayMember = "DisplayName";
很抱歉,它应该是“DisplayName”而不是“np”。请尝试一下,我建议从ValueMember中删除“np”。为了保持一致,它仍然可以工作。奇怪的是,它对ValueMember有效,而我认为它不应该。