2010-07-21 29 views
1

我需要在組合框中顯示多個數據,但我無法弄清楚如何去做。組合框的複合DisplayMember

下面的代碼我試圖讓工作:

 innerBox.DisplayMember = @"t => t.TenantName + ""\t"" + t.Property.PropertyName + ""\t"" + t.RentalUnit.UnitNumber "; 

但它不工作,這也不過:

 innerBox.DisplayMember = @"t => t.TenantName"; 

我怎樣才能獲得一個複合的工作?

+0

什麼是innerBox變量的類型嗎? – Rohit 2010-07-21 19:39:37

+0

@Rohit:我認爲這是一個組合框 – SLaks 2010-07-21 19:39:59

+0

是的,它是一個組合框。 – Malfist 2010-07-21 19:46:47

回答

2

這是不可能的。

相反,您應該添加一個屬性到您的基礎對象。

+3

+1,或覆蓋ToString()方法 – Nagg 2010-07-21 19:40:34

2

DisplayMember只能包含單個屬性名稱!如果你想要複合輸出,你應該訂閱Format事件並在代碼中編寫輸出字符串。

+0

我認爲Format事件與我的OwnerDraw DrawItem事件混淆。 – Malfist 2010-07-21 19:44:43

+0

@Malfist,如果你有老闆繪製組合框,那麼你爲什麼要困擾DisplayMember?你可以繪製任何你想要的東西,包括任何複合數據字符串。 – arbiter 2010-07-21 19:58:45

+0

是的,我想在組合框的DisplayMember中顯示格式。我怎樣才能做到這一點? – IsmailS 2011-02-08 11:26:13

0

我想有「[代碼] [文]」在DisplayMember和使用LINQ添加一個屬性解決了這個問題:

var actionCodes = pps.GetAllActionCodes(); 
if (actionCodes != null) 
{ 
    var actionCodesNew = (from c in actionCodes 
           select new 
           { 
            c.Code, 
            c.Text, 
            CodeAndDesc = string.Format("{0} {1}", c.Code, c.Text).Trim() 
           }).ToArray(); 
      comboBox.Items.AddRange(actionCodesNew); 
      comboBox.DisplayMember = "CodeAndDesc"; 
     } 
} 

工程確定,當性能不成問題:)

+0

爲了在稍後階段檢索combobox.SelectedValue,您需要將'comboBox.Items.AddRange(actionCodesNew)'行更改爲'combobox.DataSource = actionCodesNew'並設置comboBox.ValueMember =「Code」。這使c.Code成爲值成員。 – JonBaron 2011-06-30 09:51:46