combobox 添加"请选择"
/// <summary> /// 绑定combobox的数据 /// </summary> public void bind() { //从数据库获取要绑定到下拉框的数据 DataTable dt =BLL.GetData().Tables[0]; if (dtList.Rows.Count == 0) { this.ComboBox.Items.Add(new System.Collections.DictionaryEntry("--无数据--", 0)); //new System.Collections.DictionaryEntry("--无数据--", 0) --------->创建键值对 } else// { this.ComboBox.Items.Add(new System.Collections.DictionaryEntry("--请选择--", 0)); foreach (DataRow dr in dtList.Rows) { this.ComboBox.Items.Add(new System.Collections.DictionaryEntry(dr["Title"].ToString(), int.Parse(dr["ID"].ToString())));//用取出数据创建值对 } } this.ComboBox.DisplayMember = "key";//显示值 this.ComboBox.ValueMember = "value";//数据值 this.ComboBox.SelectedIndex = 0;//默认显示 } /// <summary> /// 选择事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (this.ComboBox.SelectedIndex != 0)//不是默认选项 { int vaule = int.Parse(((System.Collections.DictionaryEntry)ComboBox.SelectedItem).Value.ToString());//取得选择项的值 MessageBox.Show(vaule.ToString()); } else { //默认选项则不做任何操作 } }