我有一个代码,它将从mysql数据库填充组合框。组合框将显示状态=' YES'的所有组织。但是如果我将状态从NO更改为YES,则后续的组合框不会加载它。
这是我的组合框代码:
string sql = "SELECT orgAbbrev FROM courseandorg WHERE status = 'YES'";
conn.Open();
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
cboOrg.Items.Add(rd["orgAbbrev"].ToString());
cboOrg.ValueMember = rd["orgAbbrev"].ToString();
cboOrg.DisplayMember = rd["orgAbbrev"].ToString();
}
conn.Close();
这里是按钮的代码,我将更改状态:
try
{
conn.Open();
string sql1 = "UPDATE courseandorg SET status = @status WHERE connID = @connID";
MySqlCommand cmd1 = new MySqlCommand(sql1, conn);
cmd1.Parameters.AddWithValue("@connID", textBox1.Text);
cmd1.Parameters.AddWithValue("@status", comboBox1.Text);
cmd1.ExecuteNonQuery();
Showlist(); //This is my funtion in my datagridview
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
conn.Close();
答案 0 :(得分:0)
以这种方式更改您的代码
第一部分
private void your_func()
{
string sql = "SELECT orgAbbrev FROM courseandorg WHERE status = 'YES'";
conn.Open();
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
cboOrg.Items.Add(rd["orgAbbrev"].ToString());
cboOrg.ValueMember = rd["orgAbbrev"].ToString();
cboOrg.DisplayMember = rd["orgAbbrev"].ToString();
}
conn.Close();
}
第二部分
try
{
conn.Open();
string sql1 = "UPDATE courseandorg SET status = @status WHERE connID = @connID";
MySqlCommand cmd1 = new MySqlCommand(sql1, conn);
cmd1.Parameters.AddWithValue("@connID", textBox1.Text);
cmd1.Parameters.AddWithValue("@status", comboBox1.Text);
cmd1.ExecuteNonQuery();
Showlist(); //This is my funtion in my datagridview
your_func(); //This is new change
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}