从我的用户控件中,我尝试在名为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");
}
}
}
但列表框仅显示短信列表。应通知人,而不是我指定的实际值。在这里我设置valuemember
.它工作正常并在侧框中显示相关值。但只有列表框不显示正确的值。
这段代码有什么问题?
尝试更改此设置:
PersonList.DisplayMember = "np.FirstName" + "np.LastName";
对此:
PersonList.DisplayMember = "DisplayName";