+4 امتیاز
سلام

من میخواهم به ازای هر انتخاب combobox1 یک لیست از Table1 در combobox2 نمایش داده شود. (مثل استان و شهر)با تشکر

1 پاسخ

+4 امتیاز

سلام.

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection("ConnectionString"))
            using (SqlDataAdapter sda = new SqlDataAdapter(new SqlCommand()))
            {
                sda.SelectCommand.CommandText = "select City +' - ' + Country AS Address  FROM Table1 WHERE id = " + comboBox1.SelectedItem + "";
                sda.SelectCommand.Connection = con;
                con.Open();
                sda.SelectCommand.ExecuteNonQuery();
                con.Close();
                DataTable dt = new DataTable();
                sda.Fill(dt);
                comboBox2.DataSource = dt;
                comboBox2.DisplayMember = "Address";
            }
        }

 

روش دوم

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection("ConnectionString"))
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select City +' - ' + Country AS Address  FROM Table1 WHERE id = " + comboBox1.SelectedItem + "";
                cmd.Connection = con;
                con.Open();
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        comboBox2.Items.Add(dr["Address"].ToString());
                    }
                }
                con.Close();
            }
        }

http://stackoverflow.com/questions/17960283/how-to-get-data-from-sql-database-to-store-in-combo-box-c-sharp

سوال جدید

2,337 سوال

2,871 پاسخ

3,725 دیدگاه

3,924 کاربر

دسته بندی ها

...