ComboBox的DropDownStyle为DropDown时,在窗口加载时对其Text属性赋值没用
必须将SelectedIndex设为-1才可以,而在窗口显示后再对Text属性赋值则不需设置SelectedIndex了
奇怪的属性
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("name");
DataRow dr = dt.NewRow();
dr["id"] = "";
dr["name"] = "";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["id"] = "1";
dr["name"] = "张三";
dt.Rows.Add(dr);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "id";
comboBox1.SelectedIndex = -1;
comboBox1.Text = "不在列表中的姓名";
}
}