选中某个数据,然后用该数据去一个list中遍历出来对应的内容,赋值给textbox

解决方案 »

  1.   

    这个不用写循环,可以直接数据绑定的。
    private static string _conn = @"server=(local);database=ComboxDemo; User=sa;Password=123456;";        public Form1()
            {
                InitializeComponent();            Initializing();
            }        /// <summary>
            /// 窗体初始化
            /// </summary>
            private void Initializing()
            {
                DataTable dtComboxFields=new DataTable();            string strSql = "SELECT User_Name FROM UserInfo";            using (SqlConnection conn=new SqlConnection(_conn))
                {
                    SqlCommand cmd = new SqlCommand(strSql, conn);                SqlDataAdapter da = new SqlDataAdapter(cmd);                da.Fill(dtComboxFields);
                }            comboBox1.DisplayMember = "User_Name";
                comboBox1.DataSource = dtComboxFields;
            }        /// <summary>
            /// 下拉列表选择
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                try
                {
                    textBox1.Text = comboBox1.Text;
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }