我将我的组合框绑定到数据库,我可以从我的数据库中填充组合框。但是当我打开页面时,组合框默认包含一个值来自数据库。我想在我的组合框中显示空白,怎么做?
my code:
datatable dt;
DataAccess.Connect();
dt = DataAccess.Select("select * from CompanyMainActivity");
cmbMainActivity.DisplayMember = ("name");
cmbMainActivity.ValueMember = "MainActivityCode";
cmbMainActivity.DataSource = dt;
我尝试使用此代码,但需要付出代价
cmbMainActivity.Items.Insert(0,"从CompanyMainActivity&#34中选择名称;);
答案 0 :(得分: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;
答案 1 :(得分: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)
答案 2 :(得分:0)
假设您正在使用WPF
在xaml文件的组合框部分添加SelectedIndex
<ComboBox>
...
SelectedIndex = -1
...
</ComboBox>