要实现功能:自动选择近似选项;绑定自定义结构(数据源自xml):public class People
{
        private string _id;
        public string ID
        {
            get { return _id; }
            set { _id = value; }
        }        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
}
    如果不用DataSource,只用Items的话,可以实现自动选项,每次TextChange事件时查找(通过XPath, starts-with方法)前缀匹配的Name,然后更新Items。
    以上方法的缺点是当有重复Name时,无法获取ID。    于是,我需要邦定DataSource。因为每次编辑时,ComboBox是新创建的,所以需要动态邦定DataSource,而且这部分需要封装,不能够在ComboBox新创建时邦定特定List<People>。所以我每次TextChange事件时判断: if(comboBox.DataSource == null) 
{
    List<People> list = new List<People>();  // 必须用泛型,否则ValueMember赋值会出错。
    comboBox.DataSource = list;               // step1: 再次出发TextChange,而且会默认SelectedIndex=0,这样就将comboBox.Text修改成第一个选项的内容: "" (空串)
    comboBox.DisplayMember = "Name";
    comboBox.ValueMember = "ID";    
    ...                                       // 获取以当前ComboBox.text 为前缀的People Xmlnode
    list.add(people);
}    
    问题:
     1. 在注释step1中描述;
     2. 在调试过程中,发现Datasource中不为空时,下拉框中显示的还是空(Items是空的,是否直接原因?);
     3. 绑定了数据源的ComboBox,如果数据源中没有空串(""),是否就不能设置空呢,而是保留前一次选择的选项。     
    

解决方案 »

  1.   

    comboBox.DisplayMember = "id";试试看3. 绑定了数据源的ComboBox,如果数据源中没有空串(""),是否就不能设置空呢,而是保留前一次选择的选项。 
    可以解决
    for(int i=0;i<cbxCollege.Items.Count;i++)
    {
    if(cbxCollege.Items[i].ToString().Trim()==newCollege) {
    cbxCollege.SelectedIndex=i;
    break;
    }
    }
      

  2.   

    不知道如果采用了绑定DataSource后
    Items意味着什么To: liusaup 
    你的方法在我用Items时,不绑定自定义结构时可以实现我需要的功能。
      

  3.   


            string sql = "select name,id from Infomation ";
               
               sqlconnection  conn =new sqlconnnection("server=.;uid=sa;pwd=;database=info");
               SqlDataAdapter da = new SqlDataAdapter(sql, conn);
               DataSet ds = new DataSet();
              da.fill(ds);
            
                        //绑定数据          cboCourseName.DataSource = ds.Tables[0];
              cboCourseName.DisplayMember = "name";
              cboCourseName.ValueMember = "id";
      

  4.   

    无空串想设置为空,可以试试 SelectedIndex = -1
    第二个问题,DataSource不为Null,不见得里面就有值了,只是存在一个没元素的List对象罢了.
      

  5.   

    设置为空是想通过将输入框中文本清空,不是控制SelectedIndex,SelectedIndex应该是文本清空->回车 后的结果我说的不为空,就是Count>0
      

  6.   

    把代码贴出来
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace AutoComplete
    {
        public class Port
        {
            private string _id = "";
            public string ID
            {
                get { return _id; }
                set { _id = value; }
            }
            private string _name = "";
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
            public Port() : this("", "") { }
            public Port(string n, string i)
            {
                _name = n;
                _id = i;
            }
        }
        public partial class Form1 : Form
        {
            List<Port> _portList = new List<Port>();
            public Form1()
            {
                InitializeComponent();
                portList.DropDownStyle = ComboBoxStyle.DropDown;
            }        private void portList_TextChanged(object sender, EventArgs e)
            {
                if (portList.Text == "")
                {
                    if (portList.DroppedDown) portList.DroppedDown = false;
                }
                else
                {
                    if (!portList.DroppedDown) portList.DroppedDown = true;
                }            if (portList.DataSource == null)
                {
                    portList.DataSource = _portList;
                    portList.DisplayMember = "Name";
                    portList.ValueMember = "ID";
                }
                else
                {
                    _portList = (List<Port>)portList.DataSource;
                }            _portList.Add(new Port("name1", "id1"));
                _portList.Add(new Port("name2", "id2"));
            }
        }
    }
    private System.Windows.Forms.ComboBox portList;
      

  7.   

    if (portList.DataSource == null)
                {
                    portList.DataSource = _portList;
                    portList.DisplayMember = "Name";
                    portList.ValueMember = "ID";
                }
    _portList 有数据吗?
    好像先后出了问题吧