using (SqlConnection conn = new SqlConnection("server=127.0.0.1;database=northwind;user id=sa;password=sa;"))
{
using (SqlDataAdapter adapter = new SqlDataAdapter("select top 10 ProductID,ProductName from Products", conn))
{
DataTable table = new DataTable();
adapter.Fill(table);
comboBox1.DataSource = table;
comboBox1.DisplayMember = "ProductName";
comboBox1.ValueMember = "ProductID";
}
}ComboBox控件数据绑定和WEB应用程序DropDownList控件非常的像,只不过DropDownList是用DataTextField和DataValueField两个属性来设置文本字段和值字段,而ComboBox是用DisplayMember和ValueMember。
评论(0)
暂无评论。