string mesg=" 1|人事部 # 3|财务部 # 5|保卫科 " 
string []aa=mesg.Split('#'); 
for(i=0;i <aa.length;i++) 

    string []bb=aa[i].Split('|'); 
    //此处绑定comboBox 
} 怎么循环把bb数组的内类容绑定到comboBox 中  ,索引要是1,3,5 
而不是自己排列的0,1,2  comboBox.SelectedIndex要和选中值对得上 
有哪位大侠帮指点下下... 

解决方案 »

  1.   

    try
    string mesg=" 1|人事部 # 3|财务部 # 5|保卫科 " 
    string []aa=mesg.Split('#'); 
    for(i=0;i <aa.length;i++) 

        combox.add(i);
        string []bb=aa[i].Split('|'); 
        //此处绑定comboBox 
        combox.add(bb[i]);

      

  2.   

    // 显示部门名称(DisplayMember),返回部门代码(SelectedValue)
    private void Form1_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("code");
        dt.Columns.Add("Name");
        string mesg = " 1|人事部 # 3|财务部 # 5|保卫科 ";
        string[] aa = mesg.Split('#');
        for (int i = 0; i < aa.Length; i++)
        {
            string[] bb = aa[i].Split('|');
            dt.Rows.Add(new string[] { bb[0], bb[1] });
        }
        this.comboBox1.DataSource = dt;
        this.comboBox1.ValueMember = "Code";
        this.comboBox1.DisplayMember = "Name";
    }
    //测试用代码
    private void button3_Click(object sender, EventArgs e)
    {
        MessageBox.Show(comboBox1.SelectedValue.ToString());
    }(注:索引值是只读的,不能改)
      

  3.   

    string mesg = " 1|人事部 # 3|财务部 # 5|保卫科 ";
    string[] arr = mesg.Split(new char[] { '|', '#' });for (int i = 0; i < arr.Length; i++)
    {
        comboBox1.Items.Add(new KeyValuePair<string, string>(arr[i].Trim(), arr[i + 1].Trim()));
        i++;
    }
    comboBox1.ValueMember = "key";
    comboBox1.DisplayMember = "value";comboBox1.SelectedIndex = 0;
    Get:KeyValuePair<string, string> kvp = (KeyValuePair<string, string>)comboBox1.SelectedItem;
    MessageBox.Show(kvp.Key + "," + kvp.Value);
      

  4.   

    2楼的方法 适合从数据库取值 直接绑定数据表 comboBox1.DataSource = dt; 用到两个列名
    3楼的方法 适合固定不变常量 为每项循环赋值 for(int i;...;i++)         用到每项的 key value
    都要用ValueMember、DisplayMember两个属性,
      

  5.   

        public class ListItem
        {
            private string m_sValue = string.Empty;
            private string m_sText = string.Empty;        /// <summary>
            /// 值
            /// </summary>
            public string Value
            {
                get { return this.m_sValue; }
            }
            /// <summary>
            /// 显示的文本
            /// </summary>
            public string Text
            {
                get { return this.m_sText; }
            }        public ListItem(string value, string text)
            {
                this.m_sValue = value;
                this.m_sText = text;
            }
            public override string ToString()
            {
                return this.m_sText;
            }
            public override bool Equals(System.Object obj)
            {
                if (this.GetType().Equals(obj.GetType()))
                {
                    ListItem that = (ListItem)obj;
                    return (this.m_sText.Equals(that.Value));
                }
                return false;
            }
            public override int GetHashCode()
            {
                return this.m_sValue.GetHashCode(); ;
            }
        }                    ComBox1.Items.Add(new ListItem(mydr[1].ToString().Trim(), mydr[0].ToString().Trim()));             string value=((ListItem)ComBox1.SelectedItem).Value;
      

  6.   

    string mesg = " 1|人事部 # 3|财务部 # 5|保卫科 ";
    string[] arr = mesg.Split(new char[] { '|', '#' });for (int i = 0; i < arr.Length; i++)
    {
        comboBox1.Items.Add(new KeyValuePair<string, string>(arr[i].Trim(), arr[i + 1].Trim()));
        i++;
    }
    comboBox1.ValueMember = "key";
    comboBox1.DisplayMember = "value";comboBox1.SelectedIndex = 0;
    Get:KeyValuePair<string, string> kvp = (KeyValuePair<string, string>)comboBox1.SelectedItem;
    MessageBox.Show(kvp.Key + "," + kvp.Value);恩:之前没有看,good正解