0
我有兩個組合框是數據綁定,我認爲這是問題在這裏,但希望有人可以採取甘德,讓我知道他們的想法。我似乎無法弄清楚這一點,並嘗試了所有我可以搜索的東西。當我在combobox2中進行二級選擇時,數據不會更新,並將作爲combobox2中的主要選擇。 (對不起,超級壞在解釋這一點,但如果你需要我澄清,請讓我知道!)我有兩個組合框,組合框#2的數據不能被選中
窗體的Load與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!");
}
}
上面的圖像表明,用戶test123將被選中,但之後我點擊test123它將恢復爲mlewis412。
謝謝大家!
您在兩個組合框的'selectionchangecommitted'事件中都有相同的代碼。代碼將數據填充到combobox2中。這就是選擇被恢復的原因。 –
@ChetanRanpariya謝謝你,這是它! –