2017-07-07 10 views
0

私はここに問題があると思っていますが、だんだんと迷惑をかけて自分の考えを知らせたいと思っています。私はこのことを理解しているように見え、私が検索できるすべてを試しました。私がcombobox2で二次選択を行うと、データは更新されず、combobox2内の主要な選択肢のままになります。 (これを説明するに申し訳ありませんが、スーパー悪いが、あなたは私を明確にする必要がある場合は私に知らせてください!)ComboBox1ので私は2つのコンボボックスを持っていますが、コンボボックス#2のデータは選択できません。

フォームのLoad:comboxbox1selectionchangecomittedイベントでselectionchangecommittedイベントを使用して

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!"); 
     } 

}

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を選択することになるだろうが、それは戻ってmlewis412に戻りtest123私がクリックした後にされていることを示しています。

ありがとうございました!

+2

両方のコンボボックスの 'selectionchangecommitted'イベントに同じコードがあります。コードはcombobox2のデータを入力します。だから、選択が元に戻るのです。 –

+0

@ChetanRanpariyaありがとうございました! –

答えて

0

両方のコンボボックスのselectionchangecommittedイベントに同じコードがあります。コードはcombobox2のデータを入力します。だから、選択が元に戻るのです。 - Chetan Ranpariya

関連する問題