2014-03-13 87 views
2

我將我的組合框綁定到數據庫,我可以從我的數據庫中填充組合框。但是當我打開頁面時,組合框默認情況下包含數據庫中的一個值。我想在我的組合框中顯示空白,怎麼做?在組合框中顯示空白綁定到數據庫

my code: 

datatable dt;

DataAccess.Connect(); 
      dt = DataAccess.Select("select * from CompanyMainActivity"); 
      cmbMainActivity.DisplayMember = ("name"); 
      cmbMainActivity.ValueMember = "MainActivityCode"; 
      cmbMainActivity.DataSource = dt; 

我試試這個代碼,但dosent工作

cmbMainActivity.Items.Insert(0, 「選擇CompanyMainActivity名稱」);

回答

0

我想你在尋找什麼叫placeholder.Please參考這些鏈接可能有幫助。

[Combo box's different properties][1](http://www.dotnetperls.com/combobox) 
[If u r using wpf controls][2](http://pwlodek.blogspot.in/2009/11/watermark-effect-for-wpfs-textbox.html) 
[check this also][3](http://msdn.microsoft.com/en-us/library/windows/apps/bg182878.aspx) 
1

您可以在第一個添加空白值。

dt = DataAccess.Select("select name, MainActivityCode from CompanyMainActivity"); 

//Insert default row 
DataRow dr = dt.NewRow(); 
dr["name"] = String.Empty; 
dr["MainActivityCode"] = -1; //I have set -1 because it should not be duplicate and this record is fake. so, we can determine that the record is fake from the -1 value. 
dt.Rows.InsertAt(dr, 0); //Insert the blank record at the top of the all records. 

cmbMainActivity.DisplayMember = ("name"); 
cmbMainActivity.ValueMember = "MainActivityCode"; 
cmbMainActivity.DataSource = dt; 
0

假設你正在使用WPF

在組合框的上你的XAML文件部分中添加的SelectedIndex

<ComboBox> 
    ... 
    SelectedIndex = -1 
    ... 
</ComboBox>