ComboBox控件是一个下拉组合控件,该控件可以让用户输入或下拉选择内容。在使用的时候经常只是绑定数据表中的其中一列或把其中一列的值添加到Items中,如:name 。但在查询或使用的时候有可能需要name 所对应的ID,然而这时朋友们可能还要再用一条查询语句将name对应的ID取出来。下面我说一下我的做法提供大家参考一下。
首先新建一个windows项目,然后将ComboBox控件拖放到Form1中取名comboBox1.
下面是Form1类中的代码:
方法1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace C15Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
InitComboBox(comboBox1);
}
private void InitComboBox(ComboBox combobox)
{
combobox.DrawMode = DrawMode.Normal;//设置绘制方式
combobox.DropDownHeight = 60;
combobox.DropDownStyle = ComboBoxStyle.DropDown;
combobox.FlatStyle = FlatStyle.Flat; //设置外观
/*一般应用
combobox.Items.Add("hello");
combobox.Items.Add("word");
*/
数据绑定方式
DataTable sourcettable = new DataTable();
sourcettable.Columns.Add("ID");
sourcettable.Columns.Add("Code");
sourcettable.Columns.Add("Name");
sourcettable.Columns.Add("Telephone");
sourcettable.Columns.Add("Address");
DataRow dr1 = sourcettable.NewRow ();
dr1["ID"] = "1";
dr1["Code"] = "001";
dr1["Name"] = "张三";
dr1["Telephone"] = "12345";
dr1["Address"] = "中国广东*******";
sourcettable.Rows.Add(dr1);
DataRow dr2 = sourcettable.NewRow ();
dr2["ID"] = "2";
dr2["Code"] = "002";
dr2["Name"] = "李四";
dr2["Telephone"] = "123456";
dr2["Address"] = "中国北京*******";
sourcettable.Rows.Add(dr2);
combobox.DataSource = sourcettable;//设置数据源
combobox.DisplayMember = "Name";//设置显示列
combobox.ValueMember = "ID";//设置实际值
combobox.SelectedIndexChanged += new EventHandler(combobox_SelectedIndexChanged);
}
void combobox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox combobox = sender as ComboBox;
if (combobox != null)
{
MessageBox.Show(combobox.SelectedValue.ToString());
}
}
}
方法2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace C15Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
InitComboBox(comboBox1);
}
private void InitComboBox(ComboBox combobox)
{
combobox.DrawMode = DrawMode.Normal;//设置绘制方式
combobox.DropDownHeight = 60;
combobox.DropDownStyle = ComboBoxStyle.DropDown;
combobox.FlatStyle = FlatStyle.Flat; //设置外观
/*一般应用
combobox.Items.Add("hello");
combobox.Items.Add("word");
*/
//特殊应用
Employee employee1 = new Employee();
employee1.ID = "1";
employee1.Code = "001";
employee1.Name = "张三";
employee1.Telephone = "022123456";
employee1.Address = "中国天津××××";
Employee employee2 = new Employee();
employee2.ID = "2";
employee2.Code = "002";
employee2.Name = "李四";
employee2.Telephone = "010123456";
employee2.Address = "中国北京*******";
//将Employee对象添加到combobox控件中
combobox.Items.Add(employee1);
combobox.Items.Add (employee2 );
//将combobox控件的selectedindexchanged 事件拖管到combobox_SelectedIndexChanged事件中
combobox.SelectedIndexChanged += new EventHandler(combobox_SelectedIndexChanged);
}
//当控件选择的项发生变化时执行代码
void combobox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox combobox = sender as ComboBox;
if (combobox != null)
{
Employee employee = (Employee)combobox.SelectedItem;//获取当前选中项
if (employee != null)
{
MessageBox.Show(string.Format("ID:{0},Code:{1},Name:{2},Telephone:{3},Address:{4}", employee.ID, employee.Code, employee.Name, employee.Telephone, employee.Address));
}
}
}
}