![](/img/trans.png)
[英]System.Data.DataRowView shows in winforms combobox instead of actual data
[英]Winforms combobox displaying class names instead of actual object name
我有這個 class 包含一個 static 列表
public class TypeList
{
public string Name;
public string NameTag;
public TypeList(string Name, string NameTag)
{
this.Name = Name;
this.NameTag = NameTag;
}
public static List<TypeList> DataType = new List<TypeList>() {
new TypeList("DataType","-1"),
new TypeList("OpOne","1"),
new TypeList("OpTwo","2"),
};
}
然后我將名為 DataType 的 static 列表放入 combobox 中:
public void RefreshList()
{
List<TypeList> data = new List<TypeList>();
data = TypeList.DataType;
typeCB.DataSource = data;
typeCB.DisplayMember = "Name";
typeCB.ValueMember = "NameTag";
typeCB.SelectedValue = -1;
typeCB.SelectedText = "Select DataType";
}
但是,當我運行它時,我得到的只是 combobox 中的類名。 我的代碼有問題嗎? 我試着做
data.Select(x=>x.Name).ToList()
但這只是給了我名字部分。
我可能錯了,但根據文檔和示例,此功能可能僅適用於公共屬性獲取器,而不適用於公共字段:
獲取或設置要為此 ListControl 顯示的屬性。
public class USState
{
private string myShortName;
private string myLongName;
public USState(string strLongName, string strShortName)
{
this.myShortName = strShortName;
this.myLongName = strLongName;
}
public string ShortName
{
get
{
return myShortName;
}
}
public string LongName
{
get
{
return myLongName;
}
}
}
當然,我也建議不要將列表作為 class 類型的一部分。 一個簡單的程序 scope static 會更好。 如果是這樣的話,並且現在自動特性已經成為一種東西,這應該足以解決問題:
public class Type
{
public string Name { private set; get } ;
public string NameTag {private set; get };
public TypeList(string Name, string NameTag)
{
this.Name = Name;
this.NameTag = NameTag;
}
}
//use in the class of main, the form or some similar central point
static List<Type> TypeList = new List<Type>();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.