cmb      .DisplayMember   =   "DocTemplateName"   
                     cmb      .ValueMember   =   "DocTemplateSID"   
                     cmb      .DataSource   =   ds.Tables(0)   填充数据后,我打算根据某个值(ValueMember的值)去定位其中的项目,该如何做?    (用cmb.selectedvalue=某值

解决方案 »

  1.   

    我写了一个例子,你看一下:一个表,三个字段,"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();
                
            }
            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"
      

  2.   

    就直接用:this.comboBox1.SelectedValue =某值,不过这个"某值"必须是DocTemplateSID中的一个..