Combobox 在绑定到数据库的组合框中显示空白

Combobox 在绑定到数据库的组合框中显示空白,combobox,Combobox,我正在将combobox绑定到数据库,并且可以从数据库填充combobox。但是当我打开页面时,combobox默认包含数据库中的一个值。我想在组合框中显示空白,怎么做 my code: 数据表dt DataAccess.Connect(); dt = DataAccess.Select("select * from CompanyMainActivity"); cmbMainActivity.DisplayMember = ("name");

我正在将combobox绑定到数据库,并且可以从数据库填充combobox。但是当我打开页面时,combobox默认包含数据库中的一个值。我想在组合框中显示空白,怎么做

my code:
数据表dt

DataAccess.Connect();
            dt = DataAccess.Select("select * from CompanyMainActivity");
            cmbMainActivity.DisplayMember = ("name");
            cmbMainActivity.ValueMember = "MainActivityCode";
            cmbMainActivity.DataSource = dt;
我试过这个代码,但没有成功


cmbMainActivity.Items.Insert(0,“从公司活动中选择名称”)

我想你要找的是所谓的占位符。请参考这几个可能有用的链接

[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)

您可以首先添加一个空白值

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;

假设您正在使用WPF

在xaml文件的组合框部分添加SelectedIndex

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

...
SelectedIndex=-1
...