在组合中显示空白与数据库结合



我将我的ComboBox绑定到数据库,并且能够从数据库中填充ComboBox。但是当我打开页面时,Combobox在数据库中默认包含一个值。我想在我的组合中显示空白,该怎么做?

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,"从Company -MainActivity中选择名称");

您可以在首先添加空白值。

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;

我猜你要寻找的是所谓的座位持有人。请参考这些可能有帮助的几个链接。

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

假设您正在使用wpf

在XAML文件上的Combobox的部分添加selectedIndex

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

最新更新