如何在组合框中显示与另一个组合框项目相关的项目 [英] how to show item in combobox as related to another combobox Item

查看:62
本文介绍了如何在组合框中显示与另一个组合框项目相关的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string ADDRES = Convert.ToString(comboBox1.SelectedItem);
                SqlConnection con = new SqlConnection();
                con.ConnectionString = "Data source=.;User ID=sa;Password=password;Initial Catalog=bank";
                string sql = "select NAME_PEOPLE from PEOPLE where ADDRESS='@ADDRESS' ";
                SqlCommand cmd=new SqlCommand(sql,con);
                cmd.Parameters.AddWithValue("@ADDRESS", ADDRES);
                 con.Open();
                 SqlDataAdapter ada = new SqlDataAdapter(cmd);
                     
                 DataTable dt = new DataTable();
                    ada.Fill(dt);
                if(comboBox1.SelectedIndex==1)
                {
                        
                     string w=dt.Rows[dt.Rows.Count-1]["NAME_PEOPLE"].ToString();
                            comboBox2.DataSource = dt;
                            comboBox2.DisplayMember = "NAME_PEOPLE";
                }

推荐答案

您好



这里我添加了解决方案请检查



Hi

Here i added solution please check that

private void Form2_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("a");
            comboBox1.Items.Add("b");
            comboBox1.Items.Add("c");
            comboBox1.Items.Add("d");
            comboBox1.SelectedIndex = -1;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var selectedItem = Convert.ToString(comboBox1.SelectedItem);

            //Instead of this code you can read data from database
            DataTable dt = new DataTable();
            dt.Columns.Add("Name", typeof(string));
            for(int i=0;i<3;i++)
            {
                dt.Rows.Add(selectedItem + i);
            }


            comboBox2.DataSource = null;
            comboBox2.DataSource = dt;
            comboBox2.DisplayMember = "Name";
        }


这篇关于如何在组合框中显示与另一个组合框项目相关的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆