我有两个组合框,无法选择组合框#2的数据

时间:2017-07-07 00:27:07

标签: c# combobox

我有2个组合框,它们是数据绑定的,我认为这是问题所在,但希望有人可以采取一个雄鹅并告诉我他们的想法。我似乎无法想出这一个,并尝试了我可以搜索的一切。当我在combobox2中进行二次选择时,数据将不会更新,并将保留为combobox2中的主要选择。 (对不起,非常不好解释这个,但如果你需要我澄清请告诉我!)

使用combobox1加载表单:

    private void answers_Load(object sender, EventArgs e)
    {

        try
        {
            string conn2 = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
            //The query to use
            string query2 = "SELECT trial_id,description FROM trials";
            SqlConnection connection2 = new SqlConnection(conn2);
            //Create a Data Adapter
            SqlDataAdapter dadapter2 = new SqlDataAdapter(query2, connection2);
            //Create the dataset
            DataSet ds2 = new DataSet();
            //Open the connection
            connection2.Open();
            //Fill the Data Adapter
            dadapter2.Fill(ds2, "trials");
            connection2.Close();
            //Bind the datagridview with the data set
            comboBox1.DisplayMember = "trial_id";
            comboBox1.BindingContext = new BindingContext();
            comboBox1.ValueMember = "description";
            comboBox1.DataSource = ds2.Tables["trials"];


        }
        catch (Exception ex)
        {
            // write exception info to log or anything else
            MessageBox.Show("Error occured!");
        }

}

使用comboxbox1selectionchangecomitted事件中的selectionchangecommitted事件进行Combobox2:

    private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
    {

        #region Questions_Start

        using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT question1 FROM trials WHERE trial_id ='" + comboBox1.Text + "'";
            connection.Open();
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                    question1.Text = reader["question1"].ToString();
                connection.Close();
            }
        }

        using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT question2 FROM trials WHERE trial_id ='" + comboBox1.Text + "'";
            connection.Open();
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                    question2.Text = reader["question2"].ToString();
                connection.Close();
            }
        }



        using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT question3 FROM trials WHERE trial_id ='" + comboBox1.Text + "'";
            connection.Open();
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                    question3.Text = reader["question3"].ToString();
                connection.Close();
            }
        }



        using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT question4 FROM trials WHERE trial_id ='" + comboBox1.Text + "'";
            connection.Open();
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                    question4.Text = reader["question4"].ToString();
                connection.Close();
            }
        }


        using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT question5 FROM trials WHERE trial_id ='" + comboBox1.Text + "'";
            connection.Open();
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                    question5.Text = reader["question5"].ToString();
                connection.Close();
            }
        }



        using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT question6 FROM trials WHERE trial_id ='" + comboBox1.Text + "'";
            connection.Open();
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                    question6.Text = reader["question6"].ToString();
                connection.Close();
            }
        }

        using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT question7 FROM trials WHERE trial_id ='" + comboBox1.Text + "'";
            connection.Open();
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                    question7.Text = reader["question7"].ToString();
                connection.Close();
            }
        }



        using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT question8 FROM trials WHERE trial_id ='" + comboBox1.Text + "'";
            connection.Open();
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                    question8.Text = reader["question8"].ToString();
                connection.Close();
            }
        }


        #endregion

        #region Users_Start
        try
        {


            string conn3 = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
            //The query to use
            string query3 = "SELECT user_id FROM users_enrolled WHERE trial_id ='" + comboBox1.Text + "'";
            SqlConnection connection3 = new SqlConnection(conn3);
            //Create a Data Adapter
            SqlDataAdapter dadapter3 = new SqlDataAdapter(query3, connection3);
            //Create the dataset
            DataSet ds3 = new DataSet();
            //Open the connection
            connection3.Open();
            //Fill the Data Adapter
            dadapter3.Fill(ds3, "users_enrolled");
            connection3.Close();
            //Bind the datagridview with the data set
            comboBox2.DisplayMember = "user_id";
            comboBox2.BindingContext = new BindingContext();
            comboBox2.ValueMember = "trial_id";
            comboBox2.DataSource = ds3.Tables["users_enrolled"];


        }
        catch (Exception ex)
        {
            // write exception info to log or anything else
            MessageBox.Show("Error occured!");
        }



        #endregion

        #region Answers_Start

        using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT answer1 FROM answers WHERE trial_id ='" + comboBox1.Text + "' AND user_name ='" + comboBox2.Text + "'";
            connection.Open();
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                    answer1.Text = reader["answer1"].ToString();
                connection.Close();
            }
        }

        using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT answer2 FROM answers WHERE trial_id ='" + comboBox1.Text + "' AND user_name ='" + comboBox2.Text + "'";
            connection.Open();
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                    answer2.Text = reader["answer2"].ToString();
                connection.Close();
            }
        }


        using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT answer3 FROM answers WHERE trial_id ='" + comboBox1.Text + "' AND user_name ='" + comboBox2.Text + "'";
            connection.Open();
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                    answer3.Text = reader["answer3"].ToString();
                connection.Close();
            }
        }

        using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT answer4 FROM answers WHERE trial_id ='" + comboBox1.Text + "' AND user_name ='" + comboBox2.Text + "'";
            connection.Open();
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                    answer4.Text = reader["answer4"].ToString();
                connection.Close();
            }
        }

        using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT answer5 FROM answers WHERE trial_id ='" + comboBox1.Text + "' AND user_name ='" + comboBox2.Text + "'";
            connection.Open();
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                    answer5.Text = reader["answer5"].ToString();
                connection.Close();
            }
        }


        using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT answer6 FROM answers WHERE trial_id ='" + comboBox1.Text + "' AND user_name ='" + comboBox2.Text + "'";
            connection.Open();
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                    answer6.Text = reader["answer6"].ToString();
                connection.Close();
            }
        }


        using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT answer7 FROM answers WHERE trial_id ='" + comboBox1.Text + "' AND user_name ='" + comboBox2.Text + "'";
            connection.Open();
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                    answer7.Text = reader["answer7"].ToString();
                connection.Close();
            }
        }


        using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT answer8 FROM answers WHERE trial_id ='" + comboBox1.Text + "' AND user_name ='" + comboBox2.Text + "'";
            connection.Open();
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                    answer8.Text = reader["answer8"].ToString();
                connection.Close();
            }
        }
    }

        #endregion

Combobox2 SelectionChangeComittedEvent:

private void comboBox2_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                string conn4 = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
                //The query to use
                string query4 = "SELECT user_id FROM users_enrolled WHERE trial_id ='" + comboBox1.Text + "'";
                SqlConnection connection4 = new SqlConnection(conn4);
                //Create a Data Adapter
                SqlDataAdapter dadapter4 = new SqlDataAdapter(query4, connection4);
                //Create the dataset
                DataSet ds4 = new DataSet();
                //Open the connection
                connection4.Open();
                //Fill the Data Adapter
                dadapter4.Fill(ds4, "users_enrolled");
                connection4.Close();
                //Bind the datagridview with the data set
                comboBox2.DisplayMember = "user_id";
                comboBox2.BindingContext = new BindingContext();
                comboBox2.ValueMember = "trial_id";
                comboBox2.DataSource = ds4.Tables["users_enrolled"];

            }
            catch (Exception ex)
            {
                // write exception info to log or anything else
                MessageBox.Show("Error occured!");
            }
        }

enter image description here

上图显示将选择用户test123,但在我点击test123后,它将恢复为mlewis412。

谢谢大家!

1 个答案:

答案 0 :(得分:0)

两个组合框的selectionchangecommitted事件中的代码相同。并且代码在combobox2中填充数据。这就是选择被还原的原因。 - Chetan Ranpariya