0
從我的用戶我試圖表明一個名爲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
。它能正常工作並在sidebox中顯示相關值。但是隻有列表框不顯示正確的值。
這段代碼有什麼問題?
都能跟得上。它不工作。 – Nabil
對不起,應該是沒有「np。」的「DisplayName」,試試吧 – har07
啊......現在可行,謝謝。 – Nabil