2014-01-30 23 views
0

我的代碼結合之前默認的文本添加到組合框的窗戶形式結合,它會通過調用方法GetProperties,以PropertyCombo(組合框)如何以下

List<String> properties = CiaLogicObj.GetProperties(classComboSelectedIndex); 

BindingSource binSource = new BindingSource(); 

binSource.DataSource = properties; 

PropertyCombo.DataSource = binSource; 

得到之前綁定的屬性列表,我想我的ComboBox將顯示「<請選擇>」作爲其中的第一項。 我無法做到。 下面的方法不起作用。

PropertyCombo.text="<please Select>"PropertyCombo.Items.Insert(0,"<please Select>")

回答

0

定義一個類,

public class ComboboxItem 
    { 
     public string Text { get; set; } 
     public object Value { get; set; } 
} 

考慮您的組合框ddlService

ComboboxItem item = new ComboboxItem(); 
       item.Text = "Select Service"; 
       item.Value = -1; 
       ddlService.Items.Add(item); 

          or 
You can set default value in combobox collections in Items property. 
+0

錯誤來了: - 錯誤1「System.Windows.Forms.ComboBox」不包含「值」的定義,並沒有擴展方法「值」接受類型'System.Windows.Forms.ComboBox'的第一個參數可以被發現(你是否缺少使用指令或程序集引用?) – Amit

+0

sry我忘了提及類。 – Shashank

0

一旦您設置的綁定,ComboBox是要顯示的內容這是綁定的。設置「默認」項的方法是在列表的開頭插入一個項目:

List<String> properties = new List<String>(); 
properties.Add("<please Select"); 
foreach (String str in CiaLogicObj.GetProperties(classComboSelectedIndex)) 
    properties.Add(str); 

BindingSource binSource = new BindingSource(); 

binSource.DataSource = properties; 

PropertyCombo.DataSource = binSource; 
0

如何:

List<String> properties = CiaLogicObj.GetProperties(classComboSelectedIndex); 

properties.Insert(0, "<please Select>"); 

BindingSource binSource = new BindingSource(); 
binSource.DataSource = properties; 
PropertyCombo.DataSource = binSource; 
0

首先添加所需的值列出properties,然後添加從列表中逐個項目到PropertyCombo。最後設定的PropertyComboSelectedIndex財產0,像這樣:

List<String> properties = CiaLogicObj.GetProperties(classComboSelectedIndex); 
properties.Insert(0,"<Select Value>"); 

foreach(var item in properties) 
    PropertyCombo.Items.Add(item); 

PropertyCombo.SelectedIndex = 0;