combox 有两个属性值:ValueMember 、DisplayMember、前者对应于value、后者对应text
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using System.Data.SqlClient; 11 using System.Data.Sql; 12 13 namespace CheckedlistBox 14 { 15 public partial class Form1 : Form 16 { 17 public Form1() 18 { 19 InitializeComponent(); 20 //string strconnection = "server=172.168.1.10;database=wmts;uid=wengtai;pwd=wingtech2013"; 21 string strconnection = "server=.;database=wmts;uid=sa;pwd=test"; 22 string strcommand = "select distinct Model from FpOrderNumber"; 23 24 using (SqlConnection connection = new SqlConnection(strconnection)) 25 { 26 connection.Open(); 27 SqlCommand command = new SqlCommand(strcommand, connection); 28 SqlDataAdapter adp = new SqlDataAdapter(command); 29 DataTable dt = new DataTable(); 30 adp.Fill(dt); 31 comboBox1.DataSource = dt; 32 //使用combox.text 获取当前的值时 ValueMember,DisplayMember 二者都可以使用 33 //使用comboBox1.SelectedValue获取当前的值时,必须使用ValueMember 34 35 //comboBox1.ValueMember = "Model"; 36 comboBox1.DisplayMember = "Model"; 37 38 } 39 //button1.Click += new EventHandler(Button1_Click); 40 comboBox1.SelectedValueChanged += new EventHandler(Button1_Click); 41 42 } 43 44 private void Button1_Click(object sender, EventArgs e) 45 { 46 string strmessage = string.Empty; 47 48 //strmessage += "Combox选中:" + comboBox1.SelectedValue + " "; 49 strmessage += "Combox选中:" + comboBox1.Text + " "; 50 51 //strmessage += "Combox选中:" + comboBox1.SelectedText + " "; 52 //strmessage += "Combox选中:" + comboBox1.SelectedItem.ToString() + " "; 53 54 richTextBox1.AppendText(strmessage); 55 richTextBox1.AppendText(" "); 56 } 57 } 58 59 60 }