2013-09-28 41 views
1

綁定到列表的組合框的成員值是什麼?我使用的WinForm應用和.NET Framework 4什麼是綁定到列表的組合框的值成員<string>

cmbForms.DataSource = Forms; 
    cmbForms.ValueMember="System.String"; 
    if (!string.IsNullOrWhiteSpace(PhotoDescription.Details.Form)) 
     { 
      cmbForms.SelectedValue = PhotoDescription.Details.Form; 
     } 

其中Forms是

public List<string> Forms{ get; set; } 
+0

我想它應該是'System.String'。爲什麼不嘗試自己? –

回答

4

MSDN

如果ValueMember沒有指定屬性,則返回的SelectedValue的 結果對象的ToString方法。

編輯基於更新

你會得到一個ArgumentException與您的代碼,因爲System.String是不是可以解決的一個屬性(你string對象沒有一個叫System.String屬性)。默認值MSDN將是一個空字符串("")

在這種情況下,您不需要設置ValueMember屬性,因此您可以使用SelectedItem來代替。

cmbForms.DataSource = Forms; 
if (!string.IsNullOrWhiteSpace(PhotoDescription.Details.Form)) 
{ 
    cmbForms.SelectedItem = PhotoDescription.Details.Form; 
} 
+0

非常抱歉,我更新了我的問題。 – MegaMind

+0

沒問題,我已經更新了答案。希望有所幫助。 – keyboardP

+0

「」給我錯誤無法在ListControl中使用空的ValueMember設置SelectedValue。 – MegaMind