我写了一个例子,你看一下:一个表,三个字段,"sno","sname","sage",显示name,ValueMember是"sno",如下所示:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient;namespace TestComboBox { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.DataBind();
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace TestComboBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.DataBind();
}
private void DataBind()
{
SqlConnection con = new SqlConnection("server=.;database=student;uid=sa;pwd=0421");
SqlDataAdapter sda = new SqlDataAdapter("select * from studentDetails", con);
DataSet ds = new DataSet();
sda.Fill(ds, "student");
this.comboBox1.DataSource = ds.Tables["student"];
this.comboBox1.DisplayMember = "sname";
this.comboBox1.ValueMember = "sno";
} private void button1_Click(object sender, EventArgs e)
{
this.comboBox1.SelectedValue = "03281310";
}
}
}表中的数据如下:
03281307 liqian 20
03281308 nono 21
03281309 nonv 21
03281310 xiangzi 21当单击按钮时,显示"xiangzi"