[分享]ComboBox绑定两个字段
很多时候我们在ComboBox中显示的并不是表中的ID,而在处理数据时我们需要的是ID,如果我们在ComboBox上绑定两个字段的话,那就很方便操作了
程序代码:
private void Form1_Load(object sender, EventArgs e) { DataTable dt = GetData(); //绑定字段 = "UName"; = "UId"; = dt; } private DataTable GetData() //获取数据 { using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\db.mdb;Persist Security Info=False")) { OleDbDataAdapter adp = new OleDbDataAdapter("select * from temp", conn); conn.Open(); DataTable dt = new DataTable(); adp.Fill(dt); conn.Close(); return dt; } }
示例:
ComboBox绑定两个字段.rar
(41.36 KB)